I almost always use a Service when I download data from a web service. I store the result in a database and displays the result in my view using a cursor loader. But after Googl
I encountered the same issue, and I didn't like to have separated processes for local and network request.
I extended the Volley library to include the process of having a local database request (for offline content) and a network request.
Basically I have a CompleteRequest that will return:
an intermediate response, which can be either from http cache or from local database (run in parallel, the fastest that finishes return a result, the second one is ignored)
a final response, which will be from the network (or an error)
The request looks like:
public class SampleRequest extends CompleteRequest
And you perform the request like that, with the response listener that includes the intermediate and the final response:
mRequestQue.add(new SampleRequest(Request.Method.GET, "http://some.url", new ResponseListener() {
@Override
public void onIntermediateResponse(Object response, BallResponse.ResponseSource responseSource) {
// intermediate response, such as from local database or soft cached network response
}
@Override
public void onFinalResponse(Object response, BallResponse.ResponseSource responseSource) {
// final response, which is the network response
}
@Override
public void onFinalResponseIdenticalToIntermediate(BallResponse.ResponseSource responseSource) {
// final response is identical to intermediate one
// happens when intermediate is from soft cache and network response is identical (not modified)
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// network response is an error, in the same way than with volley
}
}
));
It's still in an early stage of development, but I'm using it in several apps and it works nice. I wrote a quick readme with examples and there is sample project that can help.
You can check it out at https://github.com/lukaspili/Volley-Ball