以下是我的代码,我跟随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;
}
根据该文章,我应该能够调用此函数并注册观察者以观察我的活动的变化,但这未显示为选项
我在做什么错?更令人困惑的是,为什么我应该成为提供MResponse
的人? MResponse
是响应的pojo类,将被翻新用于json响应的自动转换,我想观察一下。
您输入了错误的Observable
类。您已导入android.database.Observable
而不是android.database.Observable
。切换导入应该可以修复它。