Android Volley store requests when offline
Asked Answered
X

1

7

I've searched all over but haven't found an answer to this.

In my Android application, the user can use the app offline, and some events generate http GET requests to my server. I am using Volley, and when the user is online the requests work properly, but offline they fail immediately and are removed from the request queue.

I wanted Volley to store these requests, and try again when the server is accessible, or at least keep trying. Is this behaviour possible?

Here's how I'm doing things:

StringRequest request = new StringRequest(Request.Method.GET, url, listener, listener);
request.setRetryPolicy(new DefaultRetryPolicy(8000, 2, 1.5f));
postToDefaultQueue(request);

private void postToDefaultQueue (StringRequest request) {
    if (sQueue == null) {
        sQueue = Volley.newRequestQueue(mContext.get());
    }
    sQueue.add(request);
}

Thanks so much, any help appreciated


Edit

So I managed to make the request go back to the queue after it fails. The code is below:

private class DummyListener implements Response.Listener<String>, Response.ErrorListener {
    String mUrl;
    public DummyListener (String url){
        mUrl = url;
    }
    @Override
    public void onErrorResponse(final VolleyError error) {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                Log.v(DummyListener.class.getSimpleName(), "ErrorResponse", error);
                offlineStringGetRequest(mUrl);
            }
        }, 5000);
    }

    @Override
    public void onResponse(String response) {
        Log.d(DummyListener.class.getSimpleName(), "Response: " + response);
    }
}

The problem that remains is that if I kill the app, the queue gets destroyed, and the request is never made.


Edit2

Noticed some people liked this question. I ended up using Path (https://github.com/path/android-priority-jobqueue) with some modifications for the job. It worked perfectly. Just wish Volley had a native way for doing this

Xenomorphic answered 26/5, 2015 at 17:49 Comment(4)
Thanks for posting what you used finally!Eolian
No problem, hope it helps :)Xenomorphic
Hi @Luke I am trying to achieve something very similar , can you please update your code , on how you are using android-priority-jobqueue and volley together to achieve this. Thanks!Pico
Hi, unfortunately I changed companies and don't have access to that code anymore. Nevertheless, I've just checked the Path github page and it seems to be pretty well documented. Check out the calback shouldReRunOnThrowableXenomorphic
X
1

Since I found no way to do this with Volley, I ended up using Path And it worked wonders

Xenomorphic answered 12/4, 2016 at 9:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.