Timeout for server request made using "Volley" only on Android not iOS
Asked Answered
M

5

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.

Magistracy answered 6/11, 2015 at 4:50 Comment(5)
What are the api you are using with volley?. I am using OKHttpStack which extends HurlStack with volley.Kerr
@Madhukar Hebbar : I have used simple 'StringRequest' class provided by volley..!!Magistracy
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.Grandchild
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?Atlantean
Have you tried like: https://mcmap.net/q/398830/-volley-requestqueue-timeoutFactfinding
M
3

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

Magistracy answered 4/1, 2016 at 5:16 Comment(1)
Still its not working.. i put the same code as you mention in the answer.Glennieglennis
F
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!

Fuhrman answered 12/11, 2015 at 22:8 Comment(6)
How can i use this constructor with Volley.newRequestQueue(); ?Magistracy
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();Fuhrman
@ 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 ?Magistracy
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 :)Fuhrman
For your timeout issue, try reading groups.google.com/forum/#!topic/volley-users/onA_uS9KAnoFuhrman
@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?Fuhrman
M
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.

Mitrewort answered 6/11, 2015 at 4:53 Comment(0)
C
1

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

Chau answered 14/3, 2018 at 10:0 Comment(0)
E
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.

Eudoxia answered 21/1, 2018 at 1:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.