排球RequestQueue超时

时间:2014-01-22 07:52:19

标签: java android android-volley connection-timeout

RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());
mRequestQueue.add(new JsonObjectRequest(Method.GET, cityListUrl, null, new Listener<JSONObject>() 
{
    public void onResponse(JSONObject jsonResults) 
    {
        //Any Call
    }
}, new ErrorListener()
   {
        public void onErrorResponse(VolleyError arg0) 
        {
            //Any Error log
        }
   }
));

这是我的请求呼叫,我想更改或设置请求的超时。反正有可能??

5 个答案:

答案 0 :(得分:120)

您应该设置请求RetryPolicy

myRequest.setRetryPolicy(new DefaultRetryPolicy(
                MY_SOCKET_TIMEOUT_MS, 
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

这会将您的代码更改为:

RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest request = new JsonObjectRequest(Method.GET, cityListUrl, null, new
    Listener<JSONObject>() {
        public void onResponse(JSONObject jsonResults) {
            //Any Call
        }
    }, new ErrorListener(){
        public void onErrorResponse(VolleyError arg0) {
            //Any Error log
        }
    }
);


int socketTimeout = 30000;//30 seconds - change to what you want
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
request.setRetryPolicy(policy);
mRequestQueue.add(request);

如果您只是开始使用 Volley ,您可能需要考虑droidQuery,这更容易配置:

int socketTimeout = 30000;
$.ajax(new AjaxOptions().url(cityListUrl)
                        .timeout(socketTimeout)
                        .success(new Function() {
                            public void invoke($ d, Object... args) {
                                JSONObject jsonResults = (JSONObject) args[0];
                                //Any call
                            }
                        })
                        .error(new Function() {
                            public void invoke($ d, Object... args) {
                                AjaxError error = (AjaxError) args[0];
                                Log.e("Ajax", error.toString());
                            }
                        }));

答案 1 :(得分:7)

像这样的东西

RetryPolicy retryPolicy = new DefaultRetryPolicy(
    YOUR_TIMEOUT_MS,
    YOUT_MAX_RETRIES,
    YOUR_BACKOFF_MULT
);

JsonObjectRequest request = new JsonObjectRequest(...);
request.setRetryPolicy(retryPolicy);

或者您可以实现自己的RetryPolicy

答案 2 :(得分:4)

这对我有用:

RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest request = new JsonObjectRequest(Method.GET, cityListUrl, null, new
    Listener<JSONObject>() {
        public void onResponse(JSONObject jsonResults) {
            //Any Call
        }
    }, new ErrorListener(){
        public void onErrorResponse(VolleyError arg0) {
            //Any Error log
        }
    }
);


int socketTimeout = 30000;//30 seconds - change to what you want
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
request.setRetryPolicy(policy);
mRequestQueue.add(request);

答案 3 :(得分:1)

visibility:hidden;

答案 4 :(得分:1)

String url = "https://api.joind.in/v2.1/events?start=" + start + "&resultsperpage=20&format=json";
Log.i("DREG", "onLoadMoreItems: " + url);
final StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                // Add Code Here
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                    if (error instanceof NetworkError) {
                    } else if (error instanceof ServerError) {
                    } else if (error instanceof AuthFailureError) {
                    } else if (error instanceof ParseError) {
                    } else if (error instanceof NoConnectionError) {
                    } else if (error instanceof TimeoutError) {
                        Toast.makeText(getContext(),
                                "Oops. Timeout error!",
                                Toast.LENGTH_LONG).show();
                    }
            }
        }
);
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
        10000,
        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(stringRequest);