简体   繁体   English

使用Single和Rxjava不会执行改装请求吗?

[英]Using Single and Rxjava does not execute the retrofit request?

Hi i am using retrofit and rxjava to make a simple request and get the response back but it doesnt seem to be making the request itself or getting the response back? 嗨,我正在使用Retrofit和rxjava发出一个简单的请求并获得响应,但它似乎不是在发出请求本身还是在获得响应?

This is my retrofit code: 这是我的改造代码:

public class Controller

 public Single<List<ListItems>> getItems() {
        return apiCall().getItems();
    }

    private ServiceCallsApiCall() {
        OkHttpClient okHttpClient = new OkHttpClient().newBuilder().addInterceptor(interceptor).build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create()).build();

        ServiceCallsApiCall serviceCalls= retrofit.create(ServiceCallsApiCall.class);
        return foodHygieneServiceCalls;
    }

my ServiceCallsApiCall class 我的ServiceCallsApiCall类

@GET("Authorities/basic")
    Single<List<ListItems>> getItems();

Here is my Rxjava part of my code that subscribes and observes this 这是我的代码的Rxjava部分,订阅并观察到这一点

  public void getItems() {
        new Controller().getItems()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new SingleObserver<List<ListItems>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d("","onSubscribe");
                    }

                    @Override
                    public void onSuccess(List<ListItems> items) {
                        viewPresenterCallBacks.updateView(items);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d("","onError" + e.getMessage());
                    }
                });
    }

None of the onSuccess or onError gets called 没有调用onSuccess或onError

I had a similar problem recently. 我最近有一个类似的问题。 The problem is not from retrofit or rxJava, its from the deserialization of the JSON to your ListItem POJO. 问题不是出自于改造或rxJava,而是出自JSON的反序列化到ListItem POJO。 I believe the crux of the issue is that your JSON deserialization library is unable to translate the Json to POJO. 我相信问题的症结在于您的JSON反序列化库无法将Json转换为POJO。

If you are using Jackson you can just add the @JsonIgnoreProperties(ignoreUnknown = true) to your ListItem class. 如果您使用的是Jackson,则可以将@JsonIgnoreProperties(ignoreUnknown = true)到ListItem类。

In my case I was using GSON and since i wasn't interested in all the JSON properties I just changed my initial retrofit method signature from Single<Movies> getRecentMovies(); 就我而言,我使用的是GSON,由于我对所有JSON属性都不感兴趣,因此我只是从Single<Movies> getRecentMovies();更改了最初的改造方法签名Single<Movies> getRecentMovies(); to Single<ResponseBody> getRecentMovies(); Single<ResponseBody> getRecentMovies(); and extracted the desired fields in my response. 并在我的回复中提取了所需的字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM