简体   繁体   English

Android-RxJava:使用 observable 从后台线程更新 UI

[英]Android-RxJava: Update UI from background thread using observable

I just started building a simple Android app, in which I'd like to make a network request in a background thread and then update the main thread (UI thread) with the servers response.我刚刚开始构建一个简单的 Android 应用程序,我想在后台线程中发出网络请求,然后使用服务器响应更新主线程(UI 线程)。 So far I used AsyncTasks , but future implementations I'd like to use reactive Java ( RxJava ).到目前为止,我使用了AsyncTasks ,但未来的实现我想使用反应式 Java ( RxJava )。 I have never done reactive calls before, so I'd like to have a simple but complete example (Observable and Observer creation and subscription) upon which it is possible to further build on.我以前从未做过响应式调用,所以我想有一个简单但完整的示例(Observable 和 Observer 创建和订阅),可以在此基础上进一步构建。

I managed to include the RxJava dependency into the basic Android project and have written a very simple main activity using AsyncTasks for the network request.我设法将 RxJava 依赖项包含到基本的 Android 项目中,并使用 AsyncTasks 为网络请求编写了一个非常简单的主要活动。 Now I tried to substitute the AsyncTask implementation with a reactive one, but got stuck in all the information regarding the Observable and Observer.现在我尝试用响应式实现替代 AsyncTask 实现,但卡在了有关 Observable 和 Observer 的所有信息中。 I'm just not sure what exactly is necessary for a minimum but fully working example.我只是不确定最小但完全有效的示例到底需要什么。

I'd really apprechiate a bit of help in transforming the main parts into an reactive implementation, since I don't know how to handle the generation of the Observable from the response string and subscribe an Observer.我真的很想在将主要部分转换为反应式实现方面得到一些帮助,因为我不知道如何从响应字符串中处理 Observable 的生成并订阅一个观察者。

Thanks.谢谢。

package com.example.reactiveTest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private Button btnSend = null;
    private TextView result = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.btnSend = findViewById(R.id.button_send);
        this.result = findViewById(R.id.result);
    }

    public void onClickBtnSend(View view) {
        new SomeTask().execute("Just some String");
    }

    class SomeTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... strings) {
            // server request returning response String
            return response;
        }

        @Override
        protected void onPostExecute(String string) {
            // update UI with response String
            result.setText(string);
        }
    }
}

With AsyncTask, you're basically performing an asynchronous operation on a worker thread, then using its result on the main thread.使用 AsyncTask,您基本上是在工作线程上执行异步操作,然后在主线程上使用其结果。 In Rx you'd use something like the following:在 Rx 中,您将使用以下内容:

Observable.fromCallable(asyncOperation)
   .subscribeOn(backgroundThread)
   .observeOn(mainThread)
   .subscribe(result -> { /* update UI for instance */ })

It seems you're also interested in onNext , onError and onComplete .看来您也对onNextonErroronComplete感兴趣。

  • onNext is called every time the observable emits an item.每次 observable 发出一个项目时都会调用onNext Each time it's called it receives an item, and can then process it.每次调用它都会收到一个项目,然后可以处理它。
  • onError is called when the observable has encountered an error for whatever reason.当 observable 由于任何原因遇到错误时,将调用onError When it's called, it receives a Throwable, which represents the cause of the error.当它被调用时,它会收到一个 Throwable,它代表了错误的原因。 after it's called, onNext and onComplete are not called.调用后,不会调用 onNext 和 onComplete。
  • onComplete is called after onNext is called with the last item. onComplete在使用最后一项调用 onNext 之后调用。 It doesn't receive any input, you could do some clean up in it for example.它不接收任何输入,例如,您可以对其进行一些清理。

Using the above methods looks like this:使用上述方法如下所示:

Observable.fromCallable(asyncOperation)
   .subscribeOn(backgroundThread)
   .observeOn(mainThread)
   .subscribe(onNext, onError, onComplete)

[Edit] [编辑]

If you'd like to create your Observable using Observable.create() , you can definitely do that, it gives you finer control over what and when you emit through the Observable.如果您想使用Observable.create()创建您的 Observable,您绝对可以这样做,它可以让您更好地控制通过 Observable 发出的内容和时间。 You can do this for instance if you want to handle some specific errors that can result from your network request, and emit different Throwable s depending on the error.例如,如果您想处理由网络请求导致的某些特定错误,并根据错误发出不同的Throwable ,则可以执行此操作。

ObservableOnSubscribe asyncOperation = new Observable.OnSubscribe<Integer>() {
    @Override
    public void call(Subscriber<? super Integer> emitter) {
        try {
            // network request
            // Once result is ready, call emitter.onNext().
            // When done, complete this Observable by calling emitter.onComplete()
        } catch (Exception e) {
            // handle error, and emit it using emitter.onError()
        }
    }
 } 

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

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