使用Single和Rxjava不执行改造请求?

时间:2017-12-05 09:19:59

标签: android rx-java retrofit2

您好我正在使用retrofit和rxjava来发出一个简单的请求并获得响应但是它似乎没有自己提出请求或者收到回复?

这是我的改装代码:

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;
    }

我的ServiceCallsApiCall类

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

这是我的代码中的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());
                    }
                });
    }

没有调用onSuccess或onError

1 个答案:

答案 0 :(得分:0)

我最近遇到了类似的问题。问题不在于改造或rxJava,而是从JSON的反序列化到你的#include "VectorType.h" //This is a vector of std::shared_ptr<std::string> VectorType<std::string>::regular strings; //This is a const vector of std::shared_ptr<const str::string> //note that I needed to use the cast function to assign the regular one to the constant one VectorType<std::string>::constant constStrings = VectorType<std::string>::cast(strings); // You don't have to actually type it twice. Use auto. auto autoConstStrings = VectorType<std::string>::cast(strings); POJO。我相信问题的症结在于您的JSON反序列化库无法将Json转换为POJO。

如果您使用的是Jackson,则可以将ListItem添加到ListItem类中。

在我的情况下,我使用的是GSON,因为我对所有JSON属性都不感兴趣,所以我只是将我的初始改造方法签名从@JsonIgnoreProperties(ignoreUnknown = true)更改为Single<Movies> getRecentMovies();,并在我的响应中提取了所需的字段

相关问题