在RxJava 1中我做了类似的事情
private void doLogin() {
final ProjectApi service = RetrofitService.createRetrofitClient();
staticsubscription = service.service.getLogin(new PostLoginPojo("","", new Mobsess("","","","","")))
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(staticObserver);
}
Observer<ApiLogin> getLoginObserver = new Observer<ApiLogin>() {
@Override
public void onCompleted() {
staticsubscription.unsubscribe();
}
@Override
public void onError(Throwable e) {
// Called when the observable encounters an error
// Log.d(TAG, ">>>> onError gets called : " + e.getMessage());
}
@Override
public void onNext(ApiLogin staticData) {
}
};
现在使用RxJava 2 我试图像这样转换它 编辑: -
private void doLogin(){
final ProjectApi service = RetrofitService.createRetrofitClient();
Disposable disposable = service.getLogin(new PostLoginPojo("","", new Mobsess("","","","","")))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getLoginObserver());
}
public DisposableObserver<ApiLogin> getLoginObserver() {
return new DisposableObserver<ApiLogin>() {
@Override
public void onNext(ApiLogin value) {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
};
}
这是我的Project Api
public interface ProjectApi {
String login = "customer/validate";
@POST(login)
Observable<ApiLogin> getLogin(@Body PostLoginPojo post);
}
这是改造服务: -
public class RetrofitService {
private final static String API_URL = AppUrl.baseUrl ;
public RetrofitService(){
}
public static ProjectApi createRetrofitClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().
addInterceptor(interceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(client)
.build();
return retrofit.create(ProjectApi.class);
}
}
错误:incompatible types: void cannot be converted to Disposable
我收到错误“不兼容的类型”
在.subscribe(getLoginObserver())
,
要求“io.reacivex.disposbles.Disposable”找到“Void”
我在这里做错了什么? 感谢
答案 0 :(得分:3)
subscribe
现在返回void
,
public Observer<ApiLogin> getLoginObserver()
应该返回DisposableObserver<ApiLogin>
然后你应该可以使用
subscribeWith(getLoginObserver());