【问题标题】:Volley: TimeOutError排球:超时错误
【发布时间】:2018-05-25 23:13:10
【问题描述】:

我想从 URL 中获取一些数据,但服务器非常慢... 所以每当我使用 volley 获取数据时,我都会得到TimeOurError, 无论如何我可以处理多长时间凌空应该尝试获取数据,因为服务器很慢......它需要很少的时间 而且我也想不断地运行这个请求来检查数据,我打算用Timer来做……用Timer来不断地检查数据可以吗?

这是我现在的截击请求...它适用于其他 URL

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);

}

我需要连续调用这个函数(在应用程序内)

更新

我用过

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

它成功了,现在我想在我的应用程序中不断地提出这个请求......最好的方法是什么?

【问题讨论】:

标签: android android-volley


【解决方案1】:

你可以setRetryPolicy来控制volly请求超时时间。

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

看看thisQus。

【讨论】:

    【解决方案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);
    

    //此代码将有助于增加凌空请求尝试的时间。

    【讨论】:

    • 谢谢你..它工作得很好,现在你能告诉我在应用程序内连续调用这个请求的最佳方法是什么吗?
    • 尝试创建一个调用volley的方法,发送requestURL、字符串Listener和error Listener,并调用cont方法。
    • 我将获得什么?你能举个很好的例子吗?通过编辑你的答案
    【解决方案3】:

    添加这 4 行将 android volley 时间增加到 60 秒

    StringRequest myRequest   
    

    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);
    

    【讨论】:

      【解决方案4】:

      此代码将有助于增加 volley request 尝试的时间。并解决 Volley: TimeOutError

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-07
        • 2023-04-04
        • 2014-02-12
        • 1970-01-01
        • 2016-11-16
        • 2015-06-07
        • 2015-06-12
        • 2021-01-22
        相关资源
        最近更新 更多