Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Volley store requests when offline

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

like image 729
luksfarris Avatar asked May 26 '15 17:05

luksfarris


1 Answers

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

like image 152
luksfarris Avatar answered Nov 01 '22 04:11

luksfarris