我在Retrofit
使用RxJava
时尝试传递加载,成功和错误状态。我的输出由Response
Retrofit
包裹startWith
包裹,并希望使用RxJava
中的loading
方法,以便从Resource
传递startWith
状态上课。当我添加@POST
@FormUrlEncoded
Flowable<Response<ResponseMobileGetAccess>> getAccess(@Url String url, @FieldMap HashMap<String, Object> requestModel);
时,代码不起作用并且语法错误。
改造界面:
public Flowable<Resource<ResponseMobileGetAccess>> registerMobile(
@NonNull String username) {
HashMap<String, Object> body = new HashMap<>();
body.put("param1", ACTION_MOBILE_GET_ACCESS);
return Flowable
.defer(() -> localAccountMobileApiService.getAccess(ACTION_MOBILE_GET_ACCESS, body)
.map(response -> {
if (responseMobileGetAccessResponse.isSuccessful()) {
return Resource.success(responseMobileGetAccessResponse.body());
} else {
return Resource.error();
}
})).startWith(Resource.loading());
}
我用RxJava执行此方法的地方:
getGenre()
提前致谢
答案 0 :(得分:0)
我刚刚将这些代码更改为:
public Flowable<Resource<ResponseMobileGetAccess>> registerMobile(
@NonNull String username) {
HashMap<String, Object> body = new HashMap<>();
body.put("param1", ACTION_MOBILE_GET_ACCESS);
Flowable<Resource<ResponseMobileGetAccess>> defer = Flowable
.defer(() -> localAccountMobileApiService.getAccess(ACCOUNT_MOBILE_URL, body)
.map(response -> {
if (response.isSuccessful()) {
ResponseMobileGetAccess data = response.body();
if (data != null) {
if (data.getStatusCode() > 0) {
return Resource.success(data);
} else {
return Resource.serverFailed(data.getStatusText());
}
} else {
return Resource.httpFailed();
}
} else {
return Resource.httpFailed();
}
}));
return defer.startWith(Resource.loading());
}
虽然我不确定这是一种标准方式,但它确实有效。