gpt4 book ai didi

android - RxJava 和 Retrofit2 : NetworkOnMainThreadException

转载 作者:可可西里 更新时间:2023-11-01 18:49:56 28 4
gpt4 key购买 nike

我意识到我在 MainThread 上使用 subscribeOn()/observeOn()。我可以将哪些选项传递给 subscribeOn()?我可以将哪些选项传递给 observeOn()?

12-17 21:36:09.154 20550-20550/rx.test D/MainActivity2: [onCreate]
12-17 21:36:09.231 20550-20550/rx.test D/MainActivity2: starting up observable...
12-17 21:36:09.256 20550-20550/rx.test D/MainActivity2: [onError]
12-17 21:36:09.256 20550-20550/rx.test W/System.err: android.os.NetworkOnMainThreadException

政府服务.java

import java.util.List;
import retrofit.Call;
import retrofit.http.GET;
import rx.Observable;

public interface GovService {
@GET("/txt2lrn/sat/index_1.json")
Observable<MyTest> getOneTestRx();
}

MyTest.java

public class MyTest {
private String name, url;
private int num;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public int getNum() {
return num;
}

public void setNum(int num) {
this.num = num;
}

@Override
public String toString() {
return "Name: " + this.name + ", num: " + this.num + ", url: " + this.url;
}
}

MainActivity2.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;

import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MainActivity2 extends AppCompatActivity {
private final String TAG = getClass().getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "[onCreate]");
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RecyclerView mRV = (RecyclerView) findViewById(R.id.rv);
mRV.setLayoutManager(new LinearLayoutManager(this));// setup LayoutManager
mRV.setItemAnimator(new DefaultItemAnimator());// setup ItemAnimator

// setup retrofit
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://goanuj.freeshell.org")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
GovService service = retrofit.create(GovService.class);

Log.d(TAG, "starting up observable...");
Observable<MyTest> o = service.getOneTestRx();
o.subscribeOn(Schedulers.io());
o.observeOn(AndroidSchedulers.mainThread());
o.subscribe(new Subscriber<MyTest>() {
@Override
public void onCompleted() {
Log.d(TAG, "[onCompleted] ");
}

@Override
public void onError(Throwable t) {
Log.d(TAG, "[onError] ");
t.printStackTrace();
}

@Override
public void onNext(MyTest m) {
Log.d(TAG, "[onNext] " + m.toString());
}
});
}
}

最佳答案

将代码的最后一部分重写为:

service.getOneTestRx()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<MyTest>() {
@Override
public void onCompleted() {
Log.d(TAG, "[onCompleted] ");
}

@Override
public void onError(Throwable t) {
Log.d(TAG, "[onError] ");
t.printStackTrace();
}

@Override
public void onNext(MyTest m) {
Log.d(TAG, "[onNext] " + m.toString());
}
});

来自@akarnokd 的重要说明:

Worth mentioning that one needs to chain the calls as here because Observable is not the builder pattern (where you modify the settings of an existing object)

关于android - RxJava 和 Retrofit2 : NetworkOnMainThreadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34349334/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com