We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
// 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(); } }; }
The text was updated successfully, but these errors were encountered:
Duplicated from square/okhttp#1986
Sorry, something went wrong.
No branches or pull requests
// okhttpclient setting
The text was updated successfully, but these errors were encountered: