Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any way to disagree server header? #1265

Closed
simpleton opened this issue Jan 1, 2015 · 5 comments
Closed

Is there any way to disagree server header? #1265

simpleton opened this issue Jan 1, 2015 · 5 comments

Comments

@simpleton
Copy link

I want to do some cache stuff in android client, but the server always return Cache-Control: max-age=0, private, must-revalidate, I wanna ignore it's Cache-Control.

@swankjesse
Copy link
Member

@simpleton
Copy link
Author

@swankjesse thanks. :D

@simpleton
Copy link
Author

Hi swankjesse, is there any way to use this way with retrofit? I build this kind of client and set it to retrofit, but REWRITE_CACHE_CONTROL_INTERCEPTOR didn't get the callback.

    public static Client buildOkHttpClient(boolean cacheEnable) {
        Cache cache = null;
        OkHttpClient client = new OkHttpClient();
        if (cacheEnable) {
            File cacheDirectory = new File(MainApplication.getInstance().getCacheDir().getAbsolutePath(), "HttpCache");
            try {
                cache = new Cache(cacheDirectory, CACHE_SIZE);
            } catch (IOException e) {
                e.printStackTrace();
            }
            client.setCache(cache);
            client.interceptors().add(REWRITE_CACHE_CONTROL_INTERCEPTOR);
        }
        return new OkClient(client);
    }

    /** Dangerous interceptor that rewrites the server's cache-control header. */
    private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
        @Override public Response intercept(Chain chain) throws IOException {
            Response originalResponse = chain.proceed(chain.request());
            return originalResponse.newBuilder()
                    .header("Cache-Control", String.format("max-age=%d, max-stale=%d", MAX_AGE, MAX_STALE))
                    .build();
        }
    };

@swankjesse
Copy link
Member

Are you comfortable using a snapshot build of Retrofit? The Retrofit code in git supports OkHttp headers, but that hasn't been released yet. (Probably will be released within 4–8 weeks.)

@simpleton
Copy link
Author

@swankjesse Thank your replay. I change to compile 'com.squareup.retrofit:retrofit:2.0.0-SNAPSHOT' But it still can't cache the response. I take a issue in retrofit project. square/retrofit#693

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants