Bypass cache and then update it in retrofit - android

What i want to do is at a specific time bypass cache, make an http request and then update the new result back to cache for the next time I need it. Is there any way to do this with retrofit + okhttp in android??

You can force a network request by using CacheControl.FORCE_NETWORK for the request. You could either do it when creating the request, or in an interceptor.
Another way would be to use Cache.evictAll() to clear the cache completely. Any subsequent requests will be put in the cache.
You can use a Timer to schedule and trigger the eviction process.

Related

Android deferred web request

What is the best way to make a deferred simple okhttp3 web request like:
Request request = Request.Builder().url(url).post(body).build();
Response response = client.newCall(request).execute();
that gets executed as soon as a client goes online and only then to handle result?
For example, client makes change to the data locally and this change should be posted on server. But there are cases when user does it offline, but we still have to handle it.
You can use a broadcast receiver which listen for internet connectivity changes, and Deferred library for deferring the request Jdeferred.
If use of deferred is too much work and overkill, You can use a conutdownlatch and
wait on separate thread until, the connectivity change, but the wait time can be huge, so chose wisely.
You can also consider CompletableFuture. But this is available from api 24 only
Let me know if you need more clarification.

How to cache the request queue (not responses) with OkHttp?

I use OkHttp 3 on Android 4.4.
Most answers I found are about caching the content of the HTTP responses of some request.
I want to cache the requests themselves. So when I am offline for some time and then the connection gets better, all requests are sent.
How do I do this with OkHttp? Is there a build-in request queue?
I read about silent-retry but am not sure how it behaves exactly. What if my connection is bad for e.g. 6h? What if I have 30 pending requests?
You might want to look at Tape.

Android-Volley: Set retry policy on ImageLoader

I'm using Volley as network library on Android. I ran into 'limited functionality' problems when using ImageLoader. It seems to be quite useful class with caching and stuff, so I want to continue using it. However, it doesn't give any access to the Request objects it creates. As a result, I'm not able to do some stuff that I can do in other cases (like setting a tag on the request for cancelling it from queue).
My current problem is - how can I set a retry policy on requests made using ImageLoader?
I think there is no way to set retry policy for ImageLoader. But you have access to all requests through volley singleton (if you use one). Try to change retry policy in addToRequestQueue method. If you need different retry specification for images and other requests - you can simply create two request queues (bad practice).

How to get 2 request work at same time Async Task?

I have 2 class of AsyncTask for handling request one for sync and other class for handling other request but when I send sync request and move to other page and request response for second request will get after sync request responds. How I solution this?
Hope any one help me
Basically you can use Volley
Volley offers the following benefits:
Automatic scheduling of network requests.
Multiple concurrent network connections.
Transparent disk and memory response caching with standard HTTP
cache coherence.
Support for request prioritization.
Cancellation request API. You can cancel a single request, or you
can set blocks or scopes of requests to cancel.
Ease of customization, for example, for retry and backoff.
Strong ordering that makes it easy to correctly populate your UI
with data fetched asynchronously from the network.
Debugging and tracing tools.
You can easily find a tutorial for it and
It much faster then AsyncTask .
For reference check this
You can make asyntask run parallel execution by replacing execute() with executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR).

Enabling Caching for REST requests in Robospice with Jackson and Spring

I have an android app using Robospice with Jackson + Spring to perform REST requests. This is working, except that Robospice doesn't seem to be caching the responses. I've also made sure that the cache key is consistent between requests.
I'm setting up the SpiceManager like this:
private SpiceManager mRestManager = new SpiceManager(JacksonSpringAndroidSpiceService.class);
Then, I'm executing the request like this:
mRestManager.execute(customSpiceRequest, requestCacheKey,
DurationInMillis.ALWAYS, new CustomRequestListener())
I don't know if it's relevant, but my customSpiceRequest extends SpringAndroidSpiceRequest. I've made certain that requestCacheKey is identical between requests too.
Is there something else I need to do to enable caching between requests?
Indeed, RoboSpice is doing what you are asking for : you pass
DurationInMillis.ALWAYS as a parameter to execute.
This means that the data
in cache will always be considered expired. Thus, the SpiceRequest will
always perform a network call. You should just use a different
cacheDuration value when invoking execute.
Since then, Javadoc has been enhanced :
cacheExpiryDuration : duration in millisecond after which the content of the cache will be considered to be expired. For instance DurationInMillis.ALWAYS means that data in cache will always be considered expired, thus requests will always perform their network operations to get new data. DurationInMillis.NEVER means data will never be considered as expired, requests will never perform network operations to refresh data but will always return cached data. (see {#link DurationInMillis}).*

Categories

Resources