11

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

Problem : Timeout and error object is null on onErrorResponse(VolleyError error)

What i have tried so far :

1) First I got null error object so solved it by using below code :

 @Override
 protected void deliverResponse(String response) {
    super.deliverResponse(response);
 }

 @Override
 public void deliverError(VolleyError error) {
     super.deliverError(error);
     DebugLog.e("deliverResponse", "getNetworkTimeMs : " + error.getNetworkTimeMs());
 }

So far I have got that there is timeout happening when I got error object null.

2) Now Application is for Android and iOS and web but timeout happens only for Android.

Volley log for requests :

BasicNetwork.logSlowRequests: HTTP response for request

Edited Note :

  1. Web services develoed at server end is same for all three instances (Android , Web and iOS).

  2. Timeout happens when too many users makes requests to the server.

  3. I have set time out to 2 minutes though volley throws timeout in 30 seconds only sometimes.

  4. I have many answers to change server but as it is not possible so any other solution please.

I also like to add that if i can get more information about when timeout can be possible in volley ?

References I have been gone through :

Optimizing Volley

httpclient-often-times-out-using-wifi-is-going-fine-with-3g

long_xmlhttprequest_ajax_requests_timeout_on_android

Edited :

I have also set retry policy as below:

request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 48,
                0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

And also i do not want to retry if connection timeout.

How can i make efficient service call that can solve problem for timeout.

Any help will be appriciated.

Thanks.

Community
  • 1
  • 1
AndiGeeky
  • 11,266
  • 6
  • 50
  • 66
  • What are the api you are using with volley?. I am using OKHttpStack which extends HurlStack with volley. – Madhukar Hebbar Nov 06 '15 at 05:34
  • @Madhukar Hebbar : I have used simple 'StringRequest' class provided by `volley`..!! – AndiGeeky Nov 06 '15 at 06:12
  • I had similar problems and what I did is I added 0 for DefaultRetryPolicy's timeout value which seems to behave as No/Indefinite timeout. – Mike Nov 12 '15 at 22:29
  • 1
    You are running into this problem due to too many users requesting at the same time? Seems like an issue from a UX/UI perspective as well. Maybe increasing the timeout will fix the functionality issue, what about the issue of users waiting too long for a response? Users have a short attention span and don't want to stare at a spinner for too long. Maybe increasing the server's performance via Load balancing, or hardware upgrades will be a better solution? – Lucas Crawford Nov 12 '15 at 22:32
  • Have you tried like: http://stackoverflow.com/a/21762518/1318946 – Pratik Butani Nov 17 '15 at 06:43

5 Answers5

4

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.!

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
AndiGeeky
  • 11,266
  • 6
  • 50
  • 66
2

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!

Community
  • 1
  • 1
BNK
  • 23,994
  • 8
  • 77
  • 87
  • How can i use this constructor with `Volley.newRequestQueue();` ? – AndiGeeky Nov 17 '15 at 05:57
  • I think you can try the following `int threadPoolSize = 10; Cache cache = new DiskBasedCache(getCacheDir(), 5* 1024 * 1024); Network network = new BasicNetwork(new HurlStack()); RequestQueue queue = new RequestQueue(cache, network, threadPoolSize); queue.start();` – BNK Nov 17 '15 at 06:12
  • @ BNK : Tried..not solved. Again another issue. As i define timeout to be `2 minutes`, volley throws timeout before even 1 minute is not completed. like at 50 seconds. Do you have any idea about this ? – AndiGeeky Nov 18 '15 at 05:02
  • If you use Google's official volley instead of mcxiaoke's one, you can edit your BasicNetwork.java, increase `SLOW_REQUEST_THRESHOLD_MS`, try 10000 or more. About `another issue` I have no idea :) – BNK Nov 18 '15 at 06:26
  • For your timeout issue, try reading https://groups.google.com/forum/#!topic/volley-users/onA_uS9KAno – BNK Nov 18 '15 at 08:37
  • @AndiGeeky: your issue still not solved? How did you use Volley in your project? Google's one or mcxiaoke's one? If Google's and used as module (not jar file), have you tried increasing `SLOW_REQUEST_THRESHOLD_MS` in BasicNetwork.java yet? – BNK Dec 30 '15 at 09:30
1
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.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
tiny sunlight
  • 6,231
  • 3
  • 21
  • 42
0

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.

ehab
  • 129
  • 1
  • 9
0

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

Avinash
  • 361
  • 4
  • 16