java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.

最近在学习RxJava 感觉很happy ,happy的同时遇到不少问题 没办法一点一点来 上张帅图先

 

在使用RxJava + Retrofit 的时候 有时候接口请求参数不规范 

比如 {} 

@FormUrlEncoded
@POST("get_info")
Observable<String> getInfo(@Field("{}") String json);

但是怎么既能够返回
new BaseSubscribe<String>()
有能够返回
new BaseSubscribe<实体类呢?>()

查到
ScalarsConverterFactory scalarsConverterFactory = ScalarsConverterFactory.create();
.addConverterFactory(scalarsConverterFactory)来增加字符串请求

使用
GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create();
.addConverterFactory(gsonConverterFactory)增加实体类请求



但是 我这样做了却不行
后来查到 要这样才行==
可能是版本问题吧。。

GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create(new Gson());

ScalarsConverterFactory scalarsConverterFactory = ScalarsConverterFactory.create();


.baseUrl(ConstantApi.baseUrl)
.addConverterFactory(scalarsConverterFactory)
.addConverterFactory(gsonConverterFactory)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();


别忘了:
//RxJava与Retrofit的适配器
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
compile 'com.squareup.retrofit2:converter-scalars:2.1.0'



参考 :http://www.th7.cn/Program/Android/201605/851109.shtml
http://www.jianshu.com/p/dbc46cc033fb
感谢作者


最后截个图看一下目前的代码结构 很酸爽有木有。。。


 

另外关于新手使用RxJava需要注意的:

 subscriptions = new CompositeSubscription();

        service = GithubService.createGithubService(githubToken);
        view.setOnClickListener( v -> {
            Subscription sub = service.user(name)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(System.out::println);
            subscriptions.add(sub);
        });

new CompositeSubscription();来添加 每次请求返回的Subscription对象 记得在BaseActivity中subscription.unsubscribe();

参考:http://blog.chengyunfeng.com/?p=1010感谢 原作者原文中还有很多值得研究学习的东西

 

posted @ 2016-07-28 11:46  一座城池2012  阅读(17528)  评论(0编辑  收藏  举报