gpt4 book ai didi

android - map 中的翻新+ RX错误处理

转载 作者:行者123 更新时间:2023-12-03 07:58:57 26 4
gpt4 key购买 nike

如果响应失败,我想处理 map 中的错误。我尝试从本文Error handling in Retrofit使用RetrofitException,但未成功。

这是我的观察

    public Observable<JsonElement> commonResponse(final Object request, String method, final Context context) {

Log.v("method", method);
//Create Request
Map<String, Object> data = ApiHelper.commonParams(request, method, context);

return apiService.commonResponse(method, data)
.subscribeOn(Schedulers.newThread())
.observeOn(Schedulers.immediate())
.map(new Func1<Response<BaseResponse>, JsonElement>() {
@Override
public JsonElement call(Response<BaseResponse> response) {
if (!response.isSuccessful()) {
//throwException(response);

}

BaseResponse baseResponse = response.body();
return baseResponse.getData();
}
});

}

这是我的改造
    private static HttpLoggingInterceptor logging = new HttpLoggingInterceptor();

private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(ApiService.API_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create());

public static Retrofit retrofit = builder.client(httpClient.addInterceptor(logging.setLevel(HttpLoggingInterceptor.Level.BASIC)).build()).build();

public static ApiService retrofitCreate() {
return retrofit.create(ApiService.class);
}

最佳答案

添加订阅给您。可观察,我认为这可以解决您的问题。最后,您的方法添加订阅:

              .subscribe(new Subscriber<YOUR_OBJECT>() {
@Override
public void onCompleted() {

}

@Override
public void onError(Throwable e) {
//handle your error here
showError();
}

@Override
public void onNext(YOUR_OBJECT object) {
}

查看更多 here

关于android - map 中的翻新+ RX错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38933279/

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