Volley: TimeOutError

早过忘川 提交于 2019-12-12 18:41:27

问题


i want to fetch some data from a URL and the server is very slow... So whenever i use volley to get the data, I get TimeOurError, Is there anyway i can handle for how long volley should try to get the data as the server is slow...It takes little time and i also want to run this request constantly to check data, I plan to use Timer for it...Is it okay to use Timer for continuously checking data?

Here is my volley request for now...It works for other URL's

public void checkData() {
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.i("Response",response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.i("Response",error.toString());
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> map = new HashMap<String,String>();
            map.put("deviceID","none");
            return map;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

}

and i need to call this functions continuously( inside app)

UPDATE

I used

tringRequest.setRetryPolicy(new DefaultRetryPolicy(
            30000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

and it worked, Now I want to make this request continuously in my app...What is the best approach for that?


回答1:


You can setRetryPolicy for controlling volly request timeout time.

stringRequest.setRetryPolicy(new DefaultRetryPolicy(
        YOUR_TIMEOUT_MS, 
        DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

Have a look at this Qus.




回答2:


StringRequest request = new StringRequest(Request.Method.GET, requestURL, stringListener, errorListener);
        request.setRetryPolicy(new DefaultRetryPolicy(
                30000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        addToRequestQueue(request);

// this code will help to increase the time for volley request try it.




回答3:


add these 4 lines to increase the android volley time to 60 seconds

StringRequest myRequest   

or

JsonObjectRequest myRequest

.

RetryPolicy policy = new DefaultRetryPolicy(60000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
myRequest.setRetryPolicy(policy);

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(myRequest);


来源:https://stackoverflow.com/questions/47769722/volley-timeouterror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!