search

Home  >  Q&A  >  body text

android - 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 能打印出请求成功状态和返回的数据,只是回调都没走

黄舟黄舟2772 days ago675

reply all(4)I'll reply

  • PHPz

    PHPz2017-04-18 09:08:57

    I have also encountered this problem, that is, when an error occurs, the object is not returned, but a string is returned, and then it cannot be parsed. Currently, it only prompts a hard-coded error message, or directly uses string parsing. I don’t know if there is any way to solve it

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:08:57

    You can definitely get it by returning String. Post the source code. How did you write it

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 09:08:57

    If something goes wrong, handle it in onError

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 09:08:57

    The parameter passed in to getDemoData in the first method should be written incorrectly. It should be Subscriber<DemoService> subscriber,而不是Subscriber<String> subscriber

    reply
    0
  • Cancelreply