为什么不能向此Observable添加观察者?

时间:2020-01-09 21:34:54

标签: android retrofit2 rx-java2 rx-android

以下是我在this文章中遵循的代码:


    public static final String BASE_URL = "https://newsapi.org/";
    public static final String ENDPOINT = "/v2/top-headlines";
    public static final String KEY = "my_key";
    public static final String C_CODE = "us";
    // main API ="https://newsapi.org/v2/top-headlines?country=us&apiKey=my_key

   public interface ResponseClient {
        @GET(ENDPOINT)
        Observable<MResponse> getArticles(@Query("apiKey") String key, @Query("country") String countryCode);

    }
    public static Observable<MResponse> loadDataViaRetroFit() {
        Moshi moshi = new Moshi.Builder().build();
        MoshiConverterFactory pojoConvertorMoshi = MoshiConverterFactory.create(moshi);
        OkHttpClient okHttpClient= new OkHttpClient.Builder().build();
        Retrofit retrofit =
                new Retrofit.Builder().client(okHttpClient).baseUrl(BASE_URL)
                        .addConverterFactory(pojoConvertorMoshi)
                        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                        .build();

        ResponseClient client = retrofit.create(ResponseClient.class);

        Observable<MResponse> myObservable  = client.getArticles(KEY,C_CODE);

        return myObservable;

    }

根据该文章,我应该能够调用此函数并注册一个观察者以观察我的活动的变化,但这并不是一个选择。

Enter image description here

我在做什么错?更令人困惑的是,为什么我应该成为提供MResponse的人?

MResponse是用于响应的POJO类,该类将由翻新用于JSON响应的自动转换,我想观察一下。

1 个答案:

答案 0 :(得分:1)

您导入了错误的Observable类。您已导入android.database.Observable而不是io.reactivex.Observable。切换导入即可解决此问题。