Search code examples
androidretrofit2rx-java2rx-android

Why can't I add an observer to this Observable?


The following is my code, that I followed along this article:


    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;

    }

According to that article, I should have been able to call this function and register an observer for observing changes for my activity, but this is not being shown as an option.

Enter image description here

What am I doing wrong? What's more confusing is that why should I be the one providing the MResponse?

MResponse is the POJO class for the response that would be used by retrofit for auto-conversion of the JSON response, and I want to observe for it.


Solution

  • You imported the wrong Observable class. You've imported android.database.Observable instead of io.reactivex.Observable. Switching the import should fix it.