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

GzipRequestInterceptor not working #1271

Closed
chonamdoo opened this issue Nov 10, 2015 · 1 comment
Closed

GzipRequestInterceptor not working #1271

chonamdoo opened this issue Nov 10, 2015 · 1 comment

Comments

@chonamdoo
Copy link

// okhttpclient setting

    OkHttpClient statisticsApiClient = new OkHttpClient();
    statisticsApiClient.setReadTimeout(20000, TimeUnit.MILLISECONDS);
    statisticsApiClient.setConnectTimeout(20000, TimeUnit.MILLISECONDS);
    statisticsApiClient.interceptors().add(new GzipRequestInterceptor());
   RestAdapter restStatisticsAdapter = new  RestAdapter.Builder().setEndpoint("http://xxx,xxxx,com").setClient(new OkClient(statisticsApiClient))
            .setLogLevel(RestAdapter.LogLevel.FULL).build();

    // rest api
    @Headers("Cache-Control: max-age=5000")
    @POST("/api/v1/log")
    @FormUrlEncoded
    Observable<NewBaseData> postStatisticsData(@Field(ServerConst.STATISTICS_DATA) String statistics_data);


 // gzip
 public class GzipRequestInterceptor implements Interceptor {
@Override public Response intercept(Chain chain) throws IOException {
    Request originalRequest = chain.request();
    if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
        return chain.proceed(originalRequest);
    }
    Request compressedRequest = originalRequest.newBuilder()
            .header("Content-Encoding", "gzip")
            .method(originalRequest.method(), forceContentLength(gzip(originalRequest.body())))
            .build();
    return chain.proceed(compressedRequest);
}

/** https://github.com/square/okhttp/issues/350 */
private RequestBody forceContentLength(final RequestBody requestBody) throws IOException {
    final Buffer buffer = new Buffer();
    requestBody.writeTo(buffer);
    return new RequestBody() {
        @Override
        public MediaType contentType() {
            return requestBody.contentType();
        }

        @Override
        public long contentLength() {
            return buffer.size();
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.write(buffer.snapshot());
        }
    };
}

private RequestBody gzip(final RequestBody body) {
    return new RequestBody() {
        @Override public MediaType contentType() {
            return body.contentType();
        }

        @Override public long contentLength() {
            return -1; // We don't know the compressed length in advance!
        }

        @Override public void writeTo(BufferedSink sink) throws IOException {
            BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
            body.writeTo(gzipSink);
            gzipSink.close();
        }
    };
}
@JakeWharton
Copy link
Collaborator

Duplicated from square/okhttp#1986

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