Created
September 23, 2017 16:21
-
-
Save moehandi/04e1cd9b5f87ff570f68f02f8d9d3770 to your computer and use it in GitHub Desktop.
an android okhttp3 interceptor for set encoding response from server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EncodingResponseInterceptor implements Interceptor { | |
@Override | |
public okhttp3.Response intercept(Chain chain) throws IOException { | |
Request request = chain.request(); | |
okhttp3.Response response = chain.proceed(request); | |
BufferedSource source = response.body().source(); | |
source.request(Long.MAX_VALUE); // Buffer the entire body. | |
Buffer buffer = source.buffer(); | |
String responseBodyString = buffer.clone().readString(Charset.forName("EUC-KR")); | |
MediaType contentType = response.body().contentType(); | |
ResponseBody body = ResponseBody.create(contentType, responseBodyString); | |
// ResponseBody body = ResponseBody.create(contentType, jsonObject); | |
return response.newBuilder() | |
.body(body) | |
//.addHeader("Content-Type", "application/json; charset=EUC-KR") | |
.build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment