gpt4 book ai didi

android - RxJava2 & Retrofit2 服务类 HTTP 响应码

转载 作者:行者123 更新时间:2023-11-29 17:00:54 35 4
gpt4 key购买 nike

我正在使用这些版本;

compile "com.squareup.retrofit2:retrofit:2.2.0"
compile "com.squareup.retrofit2:converter-gson:2.2.0"
compile "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0"
compile "io.reactivex.rxjava2:rxjava:2.0.6"
compile "io.reactivex.rxjava2:rxandroid:2.0.1"

API服务

import io.reactivex.Observable;
import retrofit2.http.Body;
import retrofit2.http.POST;

public interface ApiService {

@POST("authenticate")
Observable<SignInResponse> auth(@Body SignInRequest request);

@POST("user/signUp")
Observable<SignUpResponse> signUp(@Body SignUpRequest request);

@POST("restricted/event/addEvent")
Observable<NewEventResponse> createEvent(@Body NewEventRequest request);
}

API 客户端

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import io.reactivex.Observable;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiClient {

private static ApiClient apiClient;
private final ApiService apiService;

public static ApiClient getInstance() {
if (apiClient == null)
apiClient = new ApiClient();
return apiClient;
}

private ApiClient() {
OkHttpClient client = new OkHttpClient.Builder()
.build();

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
apiService = retrofit.create(ApiService.class);
}

public Observable<SignInResponse> authenticate(SignInRequest request) {
return apiService.auth(request);
}

public Observable<SignUpResponse> signUp(SignUpRequest request) {
return apiService.signUp(request);
}

public Observable<NewEventResponse> createEvent(NewEventRequest request) {
return apiService.createEvent(request);
}
}

SignInPresenter

import com.okarakose.commons.mvp.presenter.BasePresenter;

import hugo.weaving.DebugLog;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;

public class SignInPresenter extends BasePresenter<SignInView> {

private final DataManager dataManager;
private final CompositeDisposable disposables;

public SignInPresenter() {
dataManager = DataManager.getInstance();
disposables = new CompositeDisposable();
}

public void signIn(String mailAddress, String password) {
getMvpView().showProgress();

mailAddress = mailAddress.trim();
password = password.trim();

SignInRequest signInRequest = new SignInRequest.Builder(mailAddress, password)
.build();

disposables.add(ApiClient.getInstance().authenticate(signInRequest)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(response -> {
getMvpView().hideProgress();

if (response.success == 1) {
dataManager.getPreferencesHelper().putToken(response.token);
dataManager.getPreferencesHelper().putUser(response.data);
getMvpView().loginSuccess();
} else {
getMvpView().loginFailed(response.messages.get(0));
dataManager.getPreferencesHelper().clearUser();
}

}, throwable -> {
throwable.printStackTrace();
getMvpView().hideProgress();
}));
}
}

Retrofit2 变更日志 https://github.com/square/retrofit/blob/master/CHANGELOG.md

RxJava 2.x is now supported with a first-party 'adapter-rxjava2' artifact.

我的问题是:如何获取像 Retrofit ResponseBody 类这样的 Http 响应代码?

另一个问题是:我们如何集中其余 api 响应?例如,如果 api 返回 http 响应代码 401,然后抛出 AuthenticationException 或成功标志返回 0,然后抛出 GenericException。

最佳答案

  • 如何获取像 Retrofit ResponseBody 类这样的 Http 响应代码?

通过返回 Observable<Response<T>>而不是 Observable<T> . Response<T>类具有访问有关响应以及 .body() 信息的方法。访问方法T .

请注意使用 Response<T> 时与直接访问 T 不同,它不会自动将非 2xx 响应代码转换为异常。 .如果服务器响应,那么它将始终调用 onNext(..) .

  • 我们如何集中休息 api 响应?

在 Retrofit 接口(interface)周围创建一个包装器,根据需要转换 Observable。例如映射 response.code() 401 错误。

关于android - RxJava2 & Retrofit2 服务类 HTTP 响应码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43031288/

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