本文介绍了NetworkOnMainThreadException 与改造 beta2 和 rxjava的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从 retroft-beta1 升级了,这很有效.我有以下 API 接口:

I recently upgraded from retroft-beta1 and this was working. I have the following interface for the API:

public interface Service {
    @POST("path")
    Observable<Object> service();
}

以及以下调用:

service.service()
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe();

它抛出一个 NetworkOnMainThreadException.但这在改造测试版1中有效.

And it throws a NetworkOnMainThreadException. But this was working in retrofit-beta1.

推荐答案

从 retrofit-beta2 开始,调用 Observable 方法 现在行为同步.所以必须使用 subscribeOn:

From retrofit-beta2, calls to Observable methods now behave synchronously. So subscribeOn must be used:

service.service()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe();

这篇关于NetworkOnMainThreadException 与改造 beta2 和 rxjava的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:54