如何在RxJava中处理`IllegalArgumentException?

时间:2015-11-05 15:30:37

标签: android retrofit rx-java

我使用with your_table as (select 1 id, 'A' name, null sub_id from dual union all select 2 id, 'B' name, 1 sub_id from dual union all select 3 id, 'C' name, 1 sub_id from dual union all select 4 id, 'D' name, 3 sub_id from dual union all select 5 id, 'E' name, 2 sub_id from dual union all select 6 id, 'F' name, 1 sub_id from dual union all select 7 id, 'G' name, 6 sub_id from dual) select * from your_table connect by prior id = sub_id start with sub_id is null order siblings by sub_id; ID NAME SUB_ID ---------- ---- ---------- 1 A 2 B 1 5 E 2 3 C 1 4 D 3 6 F 1 7 G 6 Retrofit进行网络通话,RxJava进行查看操作。在注册屏幕中,点击'注册'按钮我使用RxBinding服务将信息发布到本地服务器。

SignupActivity.class

MyApi

MyApi.class

mCompositeSubscription.add(RxView.clicks(mRegisterButton).debounce(300, TimeUnit.MILLISECONDS).
            subscribe(view -> {
              registerUser();
            }, e -> {
              Timber.e(e, "RxView ");
              onRegistrationFailed(e.getMessage());
            }));

private void registerUser() {
 mCompositeSubscription.add(api.registerUser(mEmail,
        mPassword, mConfirmPassword)
        .subscribe(user -> {
          Timber.d("Received user object. Id: " + user.getUserId());
        }, e -> {
          Timber.e(e, "registerUser() ");
          onRegistrationFailed(e.getMessage());
        }));
  }

MyService.class

 public Observable<User> registerUser(String username, String password, String confirmPassword) {
    return mService.registerUser(username, password, confirmPassword)
        .compose(applySchedulers());
  }

  @SuppressWarnings("unchecked") <T> Observable.Transformer<T, T> applySchedulers() {
    return observable -> observable.subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());
  }

由于我发布了无效信息,因此 @FormUrlEncoded @POST("users/") Observable<User> registerUser(@Path("email") String username, @Path("password") String password, @Path("password_confirmation") String confirmPassword); 来电失败。

我的主要问题是什么,IllegalArgumentException我认为IllegalArgumentException会执行RxJava,因为我的registerUser()#ErrorHandler()服务调用因异常而失败,而是调用registerUser {1}}。

如何让/ RxView#ErrorHandler()处理网络通话期间发生的异常?

1 个答案:

答案 0 :(得分:2)

我的不好,网络通话不会因IllegalArgumentException而失败,但请求构建本身因IllegalArgumentException而失败。

java.lang.IllegalArgumentException: URL "users/" does not contain {email}". 

我没有使用 @Field 注释构建POST正文,而是错误地使用了 @Path 注释。

正确的定义:

 @FormUrlEncoded @POST("users/")
 Observable<User> registerUser(@Field("email") String username,
      @Field("password") String password, @Field("password_confirmation") String confirmPassword);