Timeout for server request made using “Volley” only on Android not iOS

后端 未结 5 1373
借酒劲吻你
借酒劲吻你 2020-12-16 18:04

In one of my application, I am sending request to server using volley provided by Google.

Problem : Timeout and error object is null o

相关标签:
5条回答
  • 2020-12-16 18:20
    public class JGet extends Request {
        private final Response.Listener listener;
    
        public JGet(final String url, List params,
                    Response.Listener responseListener) {
            super(Request.Method.GET, NetUtils.getUrlWithParams(url, params), new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    NetUtils.dealVolleyError(volleyError, url);
                }
            });
            this.listener = responseListener;
            this.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 0, 1.0f));
            LogUtils.e("request-start--->");
            LogUtils.e(url);
            LogUtils.e(params);
            LogUtils.e("request-start--->");
        }
    }
    

    set timeout time.

    0 讨论(0)
  • 2020-12-16 18:26

    As i have tried to get solution of this issue for about two months, I did not get any perfect solution. Though I analyze some facts as below :

    1. You can upgrade your server's performance
    2. I have tried making web-service request using HttpURLConnection but still getting same issue over there.

    So I think this issue is not specific from volley, but you getting this issue then i would suggest to increase server performance with customizing below RetryPolicy:

    int x=2;// retry count
    request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 48,
                        x, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    

    Hope it will help.

    Suggestions are always welcome :)

    Please comment below if you found more proper solution.

    Thanks.!

    0 讨论(0)
  • 2020-12-16 18:31

    IMHO, you can refer to the following:

    Inside BasicNetwork.java, you will find some information such as:

    ...
    private static int SLOW_REQUEST_THRESHOLD_MS = 3000;
    ...
        /**
         * Logs requests that took over SLOW_REQUEST_THRESHOLD_MS to complete.
         */
        private void logSlowRequests(long requestLifetime, Request<?> request,
                byte[] responseContents, StatusLine statusLine) {
            if (DEBUG || requestLifetime > SLOW_REQUEST_THRESHOLD_MS) {
                VolleyLog.d("HTTP response for request=<%s> [lifetime=%d], [size=%s], " +
                        "[rc=%d], [retryCount=%s]", request, requestLifetime,
                        responseContents != null ? responseContents.length : "null",
                        statusLine.getStatusCode(), request.getRetryPolicy().getCurrentRetryCount());
            }
        }
    ...
    
    // if the request is slow, log it.
    long requestLifetime = SystemClock.elapsedRealtime() - requestStart;
    logSlowRequests(requestLifetime, request, responseContents, statusLine);
    ...
    

    So, if your project uses Google's volley as a module (not JAR file), you can update BasicNetwork.java, increasing SLOW_REQUEST_THRESHOLD_MS value, perhaps 10000 (ms) or more, for example.

    Another option, according to @neuron's answer at the following question:

    How to optimize network-queue-take in android Volley? (Volley Google IO 2013)

    I think you can try increase the value of NETWORK_THREAD_POOL_SIZE by using the following constructor in your app:

    public RequestQueue(Cache cache, Network network, int threadPoolSize) {
            this(cache, network, threadPoolSize,
                    new ExecutorDelivery(new Handler(Looper.getMainLooper()))); }
    

    P/S: if you only want the lines BasicNetwork.logSlowRequests: HTTP response for request not displayed anymore without increasing NETWORK_THREAD_POOL_SIZE, only need to comment (//) the line logSlowRequests... above (when your app uses Google's volley as a module - not jar file, not compile mcxiaoke... in build.gradle file)

    Hope it helps!

    0 讨论(0)
  • 2020-12-16 18:38

    Try not using the require statement to connect to your database when sending request to a PHP file using volley. I've noticed a time-out only when I use something like (require "init.php") But when I directly put my DB connection information in the same file everything seems to work just fine.

    0 讨论(0)
  • 2020-12-16 18:44

    request.setRetryPolicy(new DefaultRetryPolicy( 50000, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT))

    0 讨论(0)
提交回复
热议问题