POST request cache for Android Retrofit

What is the best way to store the retrofit of the POST request cache?

I will keep the answer and use this answer while the user is disconnected. I was given this link.
1) Upgraded using OKHttp offline cache data
2) Requests for a POST cache request with OkHttp

But in this link, the cache mechanism only works with the GET method.
- Can I store the cache in the mail request using a modified system?
- Can any library handle network cache?

thanks

+6
source share
2 answers

OkHttp support file cache

+3
source

This is the decision we have come across.

public class OnErrorRetryCache<T> { public static <T> Observable<T> from(Observable<T> source) { return new OnErrorRetryCache<>(source).deferred; } private final Observable<T> deferred; private final Semaphore singlePermit = new Semaphore(1); private Observable<T> cache = null; private Observable<T> inProgress = null; private OnErrorRetryCache(Observable<T> source) { deferred = Observable.defer(() -> createWhenObserverSubscribes(source)); } private Observable<T> createWhenObserverSubscribes(Observable<T> source) { singlePermit.acquireUninterruptibly(); Observable<T> cached = cache; if (cached != null) { singlePermit.release(); return cached; } inProgress = source .doOnCompleted(this::onSuccess) .doOnTerminate(this::onTermination) .replay() .autoConnect(); return inProgress; } private void onSuccess() { cache = inProgress; } private void onTermination() { inProgress = null; singlePermit.release(); } } 

We needed to cache the result of the HTTP request from Retrofit. Thus, it was created, with an observable, which emits one element in mind.

If the observer signed during the execution of the HTTP request, we wanted him to wait and not execute the request twice, unless he passed. To do this, the semaphore allows single access to a block that creates or returns a cached observable, and if a new observable is created, we wait until it completes.

0
source

All Articles