gpt4 book ai didi

android - 将响应返回为 Flowable 时不会调用 onComplete

转载 作者:行者123 更新时间:2023-11-29 18:54:55 25 4
gpt4 key购买 nike

我正在尝试使用改造和 rxjava 2 进行 webservie 调用。我正在探索两种不同的方法来使用 RxJava2。问题是我在使用 Observable 时收到响应,但它不适用于 Flowable。使用 Flowable 时未打印日志 我尝试对其进行调试,但它不会进入 onNextonCompleteonError。只有 onSubscribe 被执行。

1) 使用 observable 作为返回类型

new WebRequestManager().getContactObservable(userRequest)   
.subscribe(new Observer<ResponseData>() {
@Override
public void onSubscribe(Disposable d) {

}

@Override
public void onNext(ResponseData responseData) {

Log.e(TAG , "data "+responseData.getStatus());
}

@Override
public void onError(Throwable e) {

}

@Override
public void onComplete() {

Log.e(TAG , "data complete");

}
}
);

2) 使用flowable作为返回类型

new WebRequestManager().getContactFlowable(userRequest)

.subscribe(new Subscriber<ResponseData>() {
@Override
public void onSubscribe(Subscription s) {

Log.e(TAG , "contact subscription ");

}

@Override
public void onNext(ResponseData responses) {

Log.e(TAG , "contact onNext ");


}

@Override
public void onError(Throwable t) {

}

@Override
public void onComplete() {

Log.e(TAG , "contact onComplete ");
}
});

Rest contact retrofit api

public interface ContactApi {

@POST(WebRequest.GET_CONTACTS)
Flowable<ResponseData> getContactFlowable(@Body UserRequest userRequest);

@POST(WebRequest.GET_CONTACTS)
Observable<ResponseData> getContactObservable(@Body UserRequest userRequest);

}

调用网络服务

public Flowable<ResponseData> getContactsData(UserRequest userRequest){
return webRequest.getWebClient().create(ContactApi.class).getContacts(userRequest);


}

public Observable<ResponseData> getContact(UserRequest userRequest){
return webRequest.getWebClient().create(ContactApi.class).getContact(userRequest);

}

获取改造实例

 public static Retrofit getWebClient(){

//if(okHttpClient == null)
okHttpClient = new OkHttpClient.Builder()
.connectTimeout(120,TimeUnit.SECONDS)
.readTimeout(120,TimeUnit.SECONDS)
.writeTimeout(120,TimeUnit.SECONDS)

.addInterceptor(new WebRequestInterceptor("\"application/json\""))
.build();


// if(client == null)
client = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(LoganSquareConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();

return client;
}

最佳答案

对于 Subscriber,您必须调用 request 来获取项目:

new WebRequestManager().getContactFlowable(userRequest)
.subscribe(new Subscriber<ResponseData>() {
@Override
public void onSubscribe(Subscription s) {
Log.e(TAG , "contact subscription ");
s.request(Long.MAX_VALUE); // <---------------------------------
}

@Override
public void onNext(ResponseData responses) {
Log.e(TAG , "contact onNext ");
}

@Override
public void onError(Throwable t) {

}

@Override
public void onComplete() {
Log.e(TAG , "contact onComplete ");
}
});

另见 DisposableSubscriber以其为例。

关于android - 将响应返回为 Flowable 时不会调用 onComplete,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50101420/

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