du2229 2018-06-20 17:35
浏览 105

使用Volley将android应用程序连接到localhost的TimeoutError

I want to make a web service that allow my app to connect with a database in localhost. I'm trying to follow this example making some changes in the names.

In my android code I have this:

VolleySingleton.
            getInstance(getApplicationContext()).
            addToRequestQueue(new JsonObjectRequest(Request.Method.GET, Constantes.GET, null, new Response.Listener<JSONObject>() {


                @Override
                public void onResponse(JSONObject response) {
                    String result = response.toString();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    if (error.networkResponse == null) {
                        if (error.getClass().equals(TimeoutError.class)) {
                            // Show timeout error message
                            Log.e("error volley",error.getStackTrace().toString());
                            Toast.makeText(getApplicationContext(),
                                    "Oops. Timeout error!",
                                    Toast.LENGTH_LONG).show();
                        }
                    }
                }
            }

            ));

VolleySingleton class:

public final class VolleySingleton {

// Atributos
private static VolleySingleton singleton;
private RequestQueue requestQueue;
private static Context context;


private VolleySingleton(Context context) {
    VolleySingleton.context = context;
    requestQueue = getRequestQueue();
}
public static synchronized VolleySingleton getInstance(Context context) {
    if (singleton == null) {
        singleton = new VolleySingleton(context.getApplicationContext());
    }
    return singleton;
}
public RequestQueue getRequestQueue() {
    if (requestQueue == null) {
        requestQueue = Volley.newRequestQueue(context.getApplicationContext());
    }
    return requestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
    getRequestQueue().add(req);
}

The constants that I have in Constantes class:

private static final String PUERTO_HOST = "63343";
private static final String IP = "http://10.0.2.2:";
public static final String GET = IP + PUERTO_HOST + "/prueba/obtener_canales.php";

I used that ip because I read that 10.0.2.2 is for android studio emulator and 10.0.3.2 is for genymotion.

I'm using XAMPP and the route of my file is "D:\xampp\htdocs\prueba\obtener_canales.php

I'm getting all time the Toast with timeout error and when I debug in android studio there's nothing to see.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!