Java 如何将Gzip流响应Okhttp3转换为JSON

Java 如何将Gzip流响应Okhttp3转换为JSON,java,android,gzip,okhttp3,gson,Java,Android,Gzip,Okhttp3,Gson,如何将GZip转换为JSON 我的代码是: private FileContentModel getDbData(String url)throws ApiException, IOException, ConnectionException { TypeToken<FileContentModel> typeToken = new TypeToken<FileContentModel>() {}; return getData(url,

如何将GZip转换为JSON

我的代码是:

private FileContentModel getDbData(String url)throws ApiException, IOException, ConnectionException {
        TypeToken<FileContentModel> typeToken = new TypeToken<FileContentModel>() {};
        return getData(url, getPostModel(), typeToken);
}



private <T> T getData(String url, PostModel postModel, TypeToken<T> typeToken) throws ApiException, IOException, ConnectionException {
        Response response = new CallApi<T>(Connection.getOfficeApiUrl(context))
                .Post(url, postModel);

        if (response.code() != 200) throw new ApiException(context, response);

        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        T obj = gson.fromJson(response.body().charStream(),
                typeToken.getType());
        return obj;
}

public Response Post(String route, Object object) throws IOException  {

    Gson gson = new Gson();
    RequestBody body = RequestBody.create(MediaType.parse(MIME_JSON), gson.toJson(object));

    Request request = new Request.Builder()
            .url(baseUrl + route)
            .post(body)
            .addHeader("Accept-Encoding", "gzip")
            .build();

    return okHttpClient.newCall(request).execute();
}
private FileContentModel getDbData(字符串url)抛出ApiException、IOException、ConnectionException{
TypeToken TypeToken=新的TypeToken(){};
返回getData(url,getPostModel(),typeToken);
}
private T getData(字符串url、PostModel PostModel、TypeToken TypeToken)引发ApiException、IOException、ConnectionException{
Response-Response=newcallapi(Connection.getOfficeApiUrl(上下文))
.Post(url,postModel);
if(response.code()!=200)抛出新的ApiException(上下文,响应);
Gson Gson=new GsonBuilder()
.setLenient()
.create();
T obj=gson.fromJson(response.body().charStream(),
typeToken.getType());
返回obj;
}
公共响应Post(字符串路由、对象对象)引发IOException{
Gson Gson=新的Gson();
RequestBody=RequestBody.create(MediaType.parse(MIME_JSON),gson.toJson(object));
Request Request=newrequest.Builder()
.url(基本url+路由)
.职位(机构)
.addHeader(“接受编码”、“gzip”)
.build();
返回okHttpClient.newCall(request.execute();
}
谢谢

private T getData(字符串url、PostModel PostModel、TypeToken TypeToken)抛出ApiException、IOException、ConnectionException{
Response-Response=newcallapi(Connection.getOfficeApiUrl(上下文))
.Post(url,postModel);
if(response.code()!=200)抛出新的ApiException(上下文,响应);
Gson Gson=new GsonBuilder()
.setLenient()
.create();
gzip输入流gzis=新的gzip输入流(response.body().byteStream());
InputStreamReader=新的InputStreamReader(gzis);
BufferedReader in=新的BufferedReader(读卡器);
List obj=gson.fromJson(读取器,
新的TypeToken(){}.getType());
返回obj;
}
private <T> T getData(String url, PostModel postModel, TypeToken<T> typeToken) throws ApiException, IOException, ConnectionException {
        Response response = new CallApi<T>(Connection.getOfficeApiUrl(context))
                .Post(url, postModel);

        if (response.code() != 200) throw new ApiException(context, response);

        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        GZIPInputStream gzis = new GZIPInputStream(response.body().byteStream());
        InputStreamReader reader = new InputStreamReader(gzis);
        BufferedReader in = new BufferedReader(reader);
        List<BranchModel> obj =   gson.fromJson(reader,
            new TypeToken<List<BranchModel>>(){}.getType());
        return obj;
}