okhttp icon indicating copy to clipboard operation
okhttp copied to clipboard

okhttp + retrofit requset body gzip not working

Open chonamdoo opened this issue 11 years ago • 2 comments

 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

chonamdoo avatar Nov 10 '15 13:11 chonamdoo

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

iNoles avatar Nov 10 '15 16:11 iNoles

ok sorry T_T server check

chonamdoo avatar Nov 11 '15 00:11 chonamdoo