Android截击在网络速度太慢时获得错误响应

Android截击在网络速度太慢时获得错误响应,android,android-studio,android-volley,Android,Android Studio,Android Volley,嗨,当网络速度太慢时,即使请求已成功发送到服务器,我也会收到请求的错误响应?有人能帮我解决这个问题吗?设置响应超时值,如 stringRequest.setRetryPolicy(new RetryPolicy() { @Override public int getCurrentTimeout() { // Here goes the new timeout 3 minutes return 3*60*1000

嗨,当网络速度太慢时,即使请求已成功发送到服务器,我也会收到请求的错误响应?有人能帮我解决这个问题吗?

设置响应超时值,如

  stringRequest.setRetryPolicy(new RetryPolicy() {
        @Override
        public int getCurrentTimeout() {
            // Here goes the new timeout 3 minutes
            return 3*60*1000;
        }

        @Override
        public int getCurrentRetryCount() {
            // The max number of attempts
            return 5;
        }

        @Override
        public void retry(VolleyError error) throws VolleyError {

        }
    });
您应该在两行之前使用上面的超时

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

Volley对同一API进行多个调用,如果网络速度慢,会产生误导性结果。要避免这种情况,请在将请求添加到截击队列之前添加setRetryPolicy标志。

JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(………);
jsonObjReq.setRetryPolicy(新的DefaultRetryPolicy(0,DefaultRetryPolicy.DEFAULT\u MAX\u RETRIES,DefaultRetryPolicy.DEFAULT.BACKOFF\u MULT));
SingletonClass.getInstance.addToRequestQueue(JSONOBJEQ)
试试这段代码,它让api等待api响应一段时间
特定时间段(由您提供)


在stringRequest上使用retryPolicy并控制连接时间。谢谢Pavneet。但我不想retry@Ibrahim我已经添加了jsonObjRequest.setRetryPolicy(新的DefaultRetryPolicy(50000,DefaultRetryPolicy.DEFAULT\u MAX\u RETRIES,DefaultRetryPolicy.DEFAULT\u BACKOFF\u MULT));请增加时间。最大尝试次数减少设置时间50秒,重试次数为0
// time provided to wait for api response
     jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(
                    10000,
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

            // Adding request to request queue
            AppController.getInstance().addToRequestQueue(jsonObjReq, cancel_change_api);
        }