N
N
newdancer2018-03-30 12:38:08
Android
newdancer, 2018-03-30 12:38:08

How to force Retrofit to clear the received data cache?

In general, the essence of such a purchase request is fulfilled. after which the item data is cleared on the server. But at the same time, retrofit returns them along with new ones. It only helps to do cleaning in the application settings. The retrofit itself is created like this:

@Provides
    @Singleton
    Cache provideHttpCache(Application application) {
        //create cache storage
        File httpCacheDirectory = new File(application.getCacheDir(),  "responses");
        int cacheSize = 10 * 1024 * 1024; // 10 MiB
        return new Cache(httpCacheDirectory, cacheSize);
    }

    @Provides
    @Singleton
    OkHttpClient provideOkhttpClient(Cache cache) {
        return new OkHttpClient.Builder()
                .cache(cache)
                .addInterceptor(new AddCookiesInterceptor(mApplication))
                .addInterceptor(new ReceivedCookiesInterceptor(mApplication))
                .build();
    }

    @Provides
    @Singleton
    Retrofit provideRetrofit(OkHttpClient okHttpClient) {
        return new Retrofit.Builder()
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(mBaseUrl)
                .client(okHttpClient)
                .build();
    }

tried to clean the session in this way
manifest
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>

code
public static void clearCashe(Context context) {
        File dir = context.getCacheDir();
        if (dir!=null && dir.isDirectory()) {
            String[] child = dir.list();
            for (String ch : child) {
                Log.w("TEST", "delete cashe="+ch);
                File file = new File(dir, ch);
                file.delete();
            }
        }
    }

Who can advise how to deal with this?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question