android - Retrofit Rxjava 在返回 String(不是Java bean)时没有走 rx 回调?
黄舟
黄舟 2017-04-18 09:06:57
0
4
649
  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 能打印出请求成功状态和返回的数据,只是回调都没走

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(4)
PHPzhong

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

左手右手慢动作

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

大家讲道理

If something goes wrong, handle it in onError

Peter_Zhu

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!