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

问题描述 投票:0回答:1

以下是我的代码,我跟随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响应的自动转换,我想观察一下。

android retrofit2 rx-java2 rx-android
1个回答
0
投票

您输入了错误的Observable类。您已导入android.database.Observable而不是android.database.Observable。切换导入应该可以修复它。

© www.soinside.com 2019 - 2024. All rights reserved.