问题
final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(30, TimeUnit.SECONDS)
.connectTimeout(30, TimeUnit.SECONDS)
.addInterceptor(new GzipRequestInterceptor())
.build();
@Headers({
"Content-Type: application/json;charset=utf-8",
"Accept: application/json"
})
@Streaming
@POST("updateContent")
This is header I am using to get zipped Gzip response from the server.
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(BASE_URL)
.client(okHttpClient)
.build();
This is the retrofit object which I am using.
I am getting this error "JSON Document was not fully consumed"
But if I do like this it works:
ResponseHandler responseHandler = new BasicResponseHandler();
String response = (String) httpclient.execute(httpPost, responseHandler);
if (response != null && !"".equalsIgnoreCase(response)) {
String new_response = null;
byte[] zbytes = response.getBytes("ISO-8859-1");
// Add extra byte to array when Inflater is set to true
byte[] input = new byte[zbytes.length + 1];
System.arraycopy(zbytes, 0, input, 0, zbytes.length);
input[zbytes.length] = 0;
ByteArrayInputStream bin = new ByteArrayInputStream(input);
InflaterInputStream in = new InflaterInputStream(bin);
ByteArrayOutputStream bout = new ByteArrayOutputStream(1024);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(bout);
IOUtils.copy(in, bufferedOutputStream);
in.close();
bufferedOutputStream.close();
new_response = bout.toString();
responseJsonData = new JSONObject(new_response);
But I need to do this thru retrofit which is throwing the error. Enable to parse zip data coming from the server when using retrofit as gzip unzipping in automatic in retrofit.
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
来源:https://stackoverflow.com/questions/52661900/unable-to-parse-gzip-response-from-server-using-retrofit-android