zoukankan      html  css  js  c++  java
  • 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感谢 原作者原文中还有很多值得研究学习的东西

  • 相关阅读:
    JSP XML数据处理
    JSP 连接数据库
    JSP 发送邮件
    IDEA新建maven项目没有webapp目录解决方法
    web项目中idea控制台中文乱码的解决方法
    Spring基础-12-基于xml配置的事务
    Spring基础-11-事务细节
    Spring基础-10-源码分析
    Spring基础-09-事务
    Spring基础-08-jdbcTemplate
  • 原文地址:https://www.cnblogs.com/yizuochengchi2012/p/5714254.html
Copyright © 2011-2022 走看看