Retrofit Rxjava 在返回 String(不是Java bean)时没有走 rx 回调?

  1. 当 是Javabean 时就能得到回调.
    之前提问匆忙没有贴代码,好多人要是没遇上还真不知道我在说啥.

代码片段如下:

public void getDemoData(Subscriber<String> subscriber){
        HttpService.getInstance()
                .create(DemoService.class)
                .getDemoPicture("params1",params2,params3,"params4")
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);

    private void requestData() {
        HttpMethods.getInstance().getDemoData(new Subscriber<String>() {
            @Override
            public void onCompleted() {
               Toast.makeText(MainActivity.this, "ok", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Throwable e) {
                showToast(getResources().getString(R.string.error_data));
            }

            @Override
            public void onNext(String str) {
            //....
                }
            }
        });
    }
    private HttpService() {
        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
        httpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        httpClientBuilder.addInterceptor(interceptor).build();

        retrofit = new Retrofit.Builder()
                .client(httpClientBuilder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(BingUrl.BING)
                .build();
    }

HttpLoggingInterceptor 能打印出请求成功状态和返回的数据,只是回调都没走

阅读 2.4k
评论 更新于 2017-02-22
    4 个回答

    这个问题我也遇到了,就是出现错误的时候不返回对象,返回string,然后解析不了,目前只是提示写死的错误信息,或者直接用string解析,不知道有什么办法可以解决

    评论 赞赏 2017-02-21

      返回String肯定能得到,贴源码啊,你怎么写的

      评论 赞赏 2017-02-15

        出错了 就在onError里处理呀

        评论 赞赏 2017-02-22
          风云
          • 2.2k

          第一个方法中getDemoData传入的参数应该是写错了,应该是Subscriber<DemoService> subscriber,而不是Subscriber<String> subscriber

          评论 赞赏 2017-02-23
            撰写回答

            登录后参与交流、获取后续更新提醒