简体   繁体   English

带有改造2的RxJava2用于Api调用

[英]RxJava2 with retrofit 2 for Api call

In RxJava 1 I was doing something like this 在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) {


        }
    };

Now With RxJava 2 I am trying to convert it like this 现在,使用RxJava 2,我试图像这样转换它
Edited:- 编辑: -

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() {

        }
    };
}

Here is my Project Api 这是我的Project Api

public interface ProjectApi {
    String login = "customer/validate";

    @POST(login)
    Observable<ApiLogin> getLogin(@Body PostLoginPojo post);
}

And this is the Retrofit Service:- 这是翻新服务:

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);
    }
}

Error: incompatible types: void cannot be converted to Disposable 错误: incompatible types: void cannot be converted to Disposable

I am getting error "incompatible type" on .subscribe(getLoginObserver()) , 我在.subscribe(getLoginObserver())上收到错误“不兼容的类型”,

required "io.reacivex.disposbles.Disposable" found "Void" 找到“无效”所需的“ io.reacivex.disposbles.Disposable”

What I am doing wrong here? 我在这里做错了什么? Thanks 谢谢

subscribe returns void now, subscribe现在返回void

public Observer<ApiLogin> getLoginObserver() 

should return DisposableObserver<ApiLogin> and then you should be able to use 应该返回DisposableObserver<ApiLogin> ,然后您应该可以使用

subscribeWith(getLoginObserver());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM