Unable to parse gzip response from server using retrofit android

元气小坏坏 提交于 2019-12-11 15:44:17

问题


      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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!