2016-09-05 14 views
7

trước tiên tôi sẽ cố gắng giải thích những gì im đang cố gắng làm, tiếp theo bạn sẽ thấy những gì im đang thực hiện (mã). Kể từ khi im mới tại RxJava, và vẫn còn học tập rơi tự do để cho tôi ý kiến ​​của bạn.RxJava onError Không thể tạo bộ xử lý bên trong luồng chưa được gọi là Looper.prepare()

Vì vậy, im gọi API mạng từ máy chủ và khi bắt đầu yêu cầu tôi gọi bộ nạp (spinner), khi kết thúc, tôi ẩn nó và giống như khi tôi gặp lỗi. Tôi muốn điều này chung cho tất cả các yêu cầu của tôi vì vậy tôi nhận được Observable và Observer từ tham số. Trên phương pháp này, tôi chỉ quan tâm đến việc ẩn và hiển thị trình tải.

OnError (và đây là phần lừa), tôi cũng cố gắng hiển thị hộp thoại nhưng tôi đã gặp lỗi mà bạn có thể thấy trên tiêu đề. Không thể tạo xử lý bên trong chủ đề đó đã không được gọi Looper.prepare()

Đây là mã ..

protected void makeMyrequest(MyBaseActivity myBaseActivity, Observable observable, Observer observer) { 

    mSubscription = observable 
      .doOnRequest(new Action1<Long>() { 
       @Override 
       public void call(Long aLong) { 

        Log.d(TAG, "On request"); 
        myBaseActivity.showLoader(); 
       } 
      }) 
      .doOnCompleted(new Action0() { 
       @Override 
       public void call() { 
        Log.d(TAG, "onCompleted: Hide spinner"); 
        myBaseActivity.hideLoader(); 
        mSubscription.unsubscribe(); 
       } 
      }) 
      .doOnError(new Action1<Throwable>() { 
       @Override 
       public void call(Throwable throwable) { 

        Log.d(TAG, "onError: Hide spinner"); 
         myBaseActivity.showAlertDialog("error"); 
         myBaseActivity.hideLoader(); 

             } 
      }) 
      .subscribeOn(Schedulers.newThread()) 
      .observeOn(AndroidSchedulers.mainThread()) 
      .subscribe(observer); 
} 

On hoạt động cơ sở của tôi tôi có một phương pháp để hiển thị hộp thoại

public void showAlertDialog(String message) { 

    mDialog = new AlertDialog.Builder(this) 
      .setMessage(message) 
      .show(); 
} 

phần rằng vấn đề từ stacktracer

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
                       at android.os.Handler.<init>(Handler.java:200) 
                       at android.os.Handler.<init>(Handler.java:114) 
                       at android.app.Dialog.<init>(Dialog.java:119) 
                       at android.app.Dialog.<init>(Dialog.java:168) 
                       at android.support.v7.app.AppCompatDialog.<init>(AppCompatDialog.java:43) 
                       at android.support.v7.app.AlertDialog.<init>(AlertDialog.java:95) 
                       at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:927) 
                       at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:952) 
                       at xx.myapp.MyBaseActivity.showAlertDialog 
+0

Bạn cần bao gồm dấu vết ngăn xếp, nếu không, không ai có thể cho bạn biết nơi ngoại lệ bắt nguồn. – Kiskae

Trả lời

10

Bạn cần gọi số observeOn(AndroidSchedulers.mainThread()) trước doOnRequest. observeOn áp dụng cho tất cả các nhà khai thác sau khi anh ta thực hiện một chuỗi cuộc gọi. Trong trường hợp ngoại lệ của bạn được nêu ra bởi vì bạn đang cố gắng để tạo ra một hộp thoại bên ngoài của chủ đề chính.

+0

Vâng, nó hoạt động .. Cảm ơn .. Nhưng bạn có thể cho tôi một lời giải thích ngắn gọn, vì vậy tôi sẽ không lặp lại? – user1851366

+0

Đây là một bài viết hay về nó http://tomstechnicalblog.blogspot.fi/2016/02/rxjava-understanding-observeon-and.html –

Các vấn đề liên quan