gpt4 book ai didi

java - 使用 rxAndroid 的 URL.openStream networkOnMainThread

转载 作者:行者123 更新时间:2023-12-01 10:03:14 26 4
gpt4 key购买 nike

我正在使用 XmlPullParser 从服务器解析文件,当然我是在另一个线程中执行的。我正在使用 rxAndroid 来处理线程,但它抛出 NetworkOnMainThread异常(exception)。请帮助我。

public static List<ArticleModel> parseArticles(String get) {

try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
URL url = new URL(get);
**parser.setInput(url.openStream(),null);** // HERE GOES THE EXCEPTION
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {

String tagname = parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if (tagname.equalsIgnoreCase("article")) {
// create a new instance of employee
articleModel =new ArticleModel();
}
break;

case XmlPullParser.TEXT:
text = parser.getText();
break;
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase("article")) {
// add employee object to list

articleModelList.add(articleModel);
}else if (tagname.equalsIgnoreCase("id")) {
articleModel.setId(text);

} else if (tagname.equalsIgnoreCase("name")) {
articleModel.setName(text);
} else if (tagname.equalsIgnoreCase("image_url")) {
articleModel.setImage_url(text);
}
else if (tagname.equalsIgnoreCase("url")) {
articleModel.setUrl(text);
}
else if (tagname.equalsIgnoreCase("type")) {
articleModel.setType(text);
}
break;

default:
break;
}
eventType = parser.next();
}

} catch (XmlPullParserException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}

return articleModelList;
}

这是我的 RX 方法:

public void getArticles(String get, final boolean needRefresh) throws IOException {

Observable.just(XmlPullParserHanlder.parseArticles(get))
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<ArticleModel>>() {

@Override
public void onCompleted() {
}

@Override
public void onError(Throwable e) {
Log.i("SIZE", "onNext: "+ e.toString());
}

@Override
public void onNext(List<ArticleModel> articleModels) {

}
});
}

最佳答案

问题不在于从 UI 线程调用 Rx 方法。问题是您必须在非 UI 线程中执行网络操作,即 parseArticles()

您可以尝试这种方法 -

public void getArticles(String get, final boolean needRefresh) throws IOException {

Observable.create(new Observable.OnSubscribe<List< ArticleModel >>() {
@Override
public void call(Subscriber<? super List< ArticleModel >> subscriber) {

subscriber.onNext(parseArticles(get));
subscriber.onCompleted();

}
}).subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<ArticleModel>>() {

@Override
public void onCompleted() {
}

@Override
public void onError(Throwable e) {
Log.i("SIZE", "onNext: "+ e.toString());
}

@Override
public void onNext(List<ArticleModel> articleModels) {

}
});
}
}

关于java - 使用 rxAndroid 的 URL.openStream networkOnMainThread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36655687/

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