无法解析方法订阅Retrofit Rxjava android

时间:2017-04-17 04:58:44

标签: android retrofit2 rx-java2

在我的应用程序中,我尝试使用Retrofit 2RxJava2从我的网络服务器获取数据。但是当我订阅我的API接口方法时,我得到了错误

Cannot Resolve method 'subscribe(anonymous org.reactivestreams.Subscriber<*Model Name*>

API客户端:

public class ApiClient {
    public static final String BASE_URL = "*********";
    private static Retrofit retrofit = null;
    public static Retrofit getClient() {
        final OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .readTimeout(60, TimeUnit.SECONDS)
                .connectTimeout(60, TimeUnit.SECONDS)
                .build();
        if (retrofit==null) {
            Gson gson = new GsonBuilder()
                    .setLenient()
                    .create();
            retrofit = new Retrofit.Builder()
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .baseUrl(BASE_URL)
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();
        }
        return retrofit;
    }


}

ApiInterface

public interface ApiInterface {
    @FormUrlEncoded
    @POST("user/get_user")
    Observable<UserResponse> getUsers(@Field("email") String email, @Field("password") String password);

}

UserResponse型号:

public class UserResponse {

    private List<User> result;
    private String status_message;
    private int status_code;

    public int getStatus_code() {
        return status_code;
    }

    public void setStatus_code(int status_code) {
        this.status_code = status_code;
    }

    public List<User> getResult() {
        return result;
    }

    public void setResult(List<User> result) {
        this.result = result;
    }

    public String getStatus_message() {
        return status_message;
    }

    public void setStatus_message(String status_message) {
        this.status_message = status_message;
    }
}

方法内部活动:

/*Test Login */
private void loginUser(){
        ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
        getUsers = apiInterface.getUsers("xyz@gmail.com","12345678");
        getUsers.subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Subscriber<UserResponse>(){

                    @Override
                    public void onSubscribe(Subscription s) {

                    }

                    @Override
                    public void onNext(UserResponse userResponse) {

                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

2 个答案:

答案 0 :(得分:12)

Subscriber用于订阅Flowable

对于Observable,您可以使用Observer

.subscribe(new Observer<Void>() {
   @Override
   public void onSubscribe(Disposable d) {}

   @Override
   public void onNext(Void orderValues) {}

   @Override
   public void onError(Throwable e) {}

  @Override
  public void onComplete() {}
});

答案 1 :(得分:3)

您不能在V2中使用Subscriber和Observable。 使用新观察者&lt;&gt;代替