我正在使用RxAndroid,并且由于两个observable,我想执行一个改装请求。所以我使用的是zip运算符
Observable<RequestBuilder> obs = a.zipWith(b, (lhs, rhs) -> builder.b(rhs).a(lhs));
现在问题在于builder.build()返回另一个可观察的 ObservableRequest ,但是这个方法现在使它成为 ObservableObservableRequest ,我似乎无法弄清楚。我已经这样做了,但我想正确地做到这一点......
obs.map(
(builder) -> builder.build(api)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::setData)
);
也许像
obs.map((builder) -> builder.build(api))
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::setData);
答案 0 :(得分:1)
DeBuGGeR,这可能会有所帮助。
login
类中的 RestPresenter
方法
public void login(String email, String password) {
mRestService.getApi()
.postAuth(email, password, "") //create Observable
.timeout(TIMEOUT_IN_SECONDS, TimeUnit.SECONDS) //time out
.retry(RETRY_COUNT_FOR_REQUEST) //times to retry
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<AuthBean>() { // your model
@Override
public void onCompleted() {
// do nothing
}
@Override
public void onError(Throwable e) {
mView.showError("Error");
}
@Override
public void onNext(AuthBean posts) {
mView.workWithLoginResponse(posts);
}
});
}
在activity
RestService mRestService = new RestService();
mRestPresenter = new RestPresenter(YourActivity.this, mRestService);
mRestPresenter.login(email, password);
RestService
类可能看起来像这样
public class RestService {
private ProviderApi mProviderApi;
private long CONNECTION_TIMEOUT_IN_SECONDS = 15L;
private long READ_TIMEOUT_IN_SECONDS = 15L;
private long WRITE_TIMEOUT_IN_SECONDS = 25L;
public RestService() {
RequestInterceptor requestInterceptor = new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
request.addHeader("Accept", "application/json");
}
};
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(Config.URL_SERVER)
.setRequestInterceptor(requestInterceptor)
.setLogLevel(RestAdapter.LogLevel.FULL)
.setClient(new OkClient(getClient())) // need square/okhttp
.build();
mProviderApi = restAdapter.create(ProviderApi.class);
}
private OkHttpClient getClient() {
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(CONNECTION_TIMEOUT_IN_SECONDS, TimeUnit.MINUTES);
client.setReadTimeout(READ_TIMEOUT_IN_SECONDS, TimeUnit.MINUTES);
client.setWriteTimeout(WRITE_TIMEOUT_IN_SECONDS, TimeUnit.MINUTES);
return client;
}
public ProviderApi getApi() {
return mProviderApi;
}
}
mView
是您的activity
,因此您只需创建workWithLoginResponse
方法,并通过服务器答案执行您想要的操作。
此外,ProviderApi
是界面,可能包含以下内容:
@FormUrlEncoded
@POST("/auth")
Observable<AuthBean>
postAuth(@Field("login") String login,
@Field("password") String password);
答案 1 :(得分:0)
正在返回ObservableObservableRequest,但我想要 ObservableRequest将被退回
flatMap
怎么样?
//create Observable<Observable<Integer>>
Observable.just(Observable.just(1, 2, 3))
//convert to Observable<Integer>
.flatMap(integerObservable -> integerObservable)
//receive Integer values
.subscribe(integer -> {
//do something with Integer
});