gpt4 book ai didi

android - 使用 Retrofit 2 + RxJava 2 接收 Gzip JSON 响应

转载 作者:行者123 更新时间:2023-11-29 02:33:44 28 4
gpt4 key购买 nike

根据 this answer,我正在尝试接收一些数据作为 gzipped json作者:Jesse Wilson OkHttp 自动解压缩,我无需执行任何操作。

所以我的问题是,我可以像这样接收压缩后的 Json 吗? :

apiService.getMessages().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSubscriber<MessagesGet>() {
@Override
public void onNext(MessagesGet messagesGet) {



Timber.d("GET MESSAGES DATA READY TO HANDLE");

}

@Override
public void onError(Throwable t) {

Timber.e("ERROR GETTING MESSAGES");

}

@Override
public void onComplete() {

Timber.e("GETMESSAGES COMPLETED");

}
});

@Headers({
"Content-Type: application/json;charset=utf-8",
"Accept: application/json"
})
@GET("getmessages")
Flowable<MessagesGet> getMessages();

最佳答案

正如所讨论的..

Service interface. where you declare APIs

@Headers("Content-Type: application/x-gzip")
@GET(Apis.GET_PATH)
Single<Response<ResponseBody>> downloadGzip(@Path(value = "path", encoded = true) String path);

Retrofit + RxJava config

public <S> S createService(Class<S> serviceClass) {


OkHttpClient.Builder mHttpClient = new OkHttpClient.Builder();
mHttpClient.connectTimeout(60, TimeUnit.SECONDS);
mHttpClient.readTimeout(60, TimeUnit.SECONDS);
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();

if (BuildConfig.DEBUG) {
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
} else {
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
}

mHttpClient.addInterceptor(logging);

return new Retrofit.Builder()
.baseUrl(baseURL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(mHttpClient.build())
.build()
.create(serviceClass);

}

Trigger GZIP Download

ApiService apiService = serviceGenerator.createServiceForm(ApiService.class);

apiService.downloadGzip(apiEndPoint) // apiEndPoint is direct download URL of GZIP
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<Response<ResponseBody>>() {
@Override
public void onSubscribe(Disposable disposable) {
AppLogger.i(LOGGER, "gzip download ->" + "subscribed");
}

@Override
public void onSuccess(Response<ResponseBody> responseBodyResponse) {
//responseBodyResponse contains gzip
AppLogger.i(LOGGER, "gzip download -> " + " success");


// read gzip
ByteArrayInputStream bais = null;
try {
bais = new ByteArrayInputStream(responseBodyResponse.body().bytes());

GZIPInputStream gzis = new GZIPInputStream(bais);
InputStreamReader reader = new InputStreamReader(gzis);
BufferedReader in = new BufferedReader(reader);

String readed;
StringBuilder gzipResponseString = new StringBuilder();
while ((readed = in.readLine()) != null) {
gzipResponseString.append(readed); //write gzip data in StringBuilder
}

AppLogger.i(LOGGER, "Gzip string->" + gzipResponseString);


} catch (IOException e) {
AppLogger.e(LOGGER, "gzip extract exception->" + e.getLocalizedMessage(), e);
}
}

@Override
public void onError(Throwable throwable) {
AppLogger.e(LOGGER, "gzip failed->" + throwable.getMessage(), throwable);
}
});

您可以根据您的要求继续 DisposableSubscriber

关于android - 使用 Retrofit 2 + RxJava 2 接收 Gzip JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48182317/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com