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

okhttp + retrofit requset body gzip not working #1986

Closed
chonamdoo opened this issue Nov 10, 2015 · 2 comments
Closed

okhttp + retrofit requset body gzip not working #1986

chonamdoo opened this issue Nov 10, 2015 · 2 comments

Comments

@chonamdoo
Copy link

 i use  com.squareup.okhttp:okhttp:2.5.0  , com.squareup.retrofit:retrofit:1.9.0      


        // 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://test.couchgram.com").setClient(new OkClient(statisticsApiClient))
                .setLogLevel(RestAdapter.LogLevel.FULL).build();
        statisticsService = restStatisticsAdapter.create(RequestService.class);

        // 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();
                    }
                };
            }
        }

        HTTP 416 error
@iNoles
Copy link
Contributor

iNoles commented Nov 10, 2015

416 = Requested Range not satisfiable
the server is looking to right amount of bytes to match. If it is not, you get 416 error.

@chonamdoo
Copy link
Author

ok sorry T_T server check

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