NetworkOnMainThreadException with retrofit-beta2 and rxjava

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 15:48:36

问题


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();
}

And the following call:

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

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


回答1:


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

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


来源:https://stackoverflow.com/questions/33390532/networkonmainthreadexception-with-retrofit-beta2-and-rxjava

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!