【发布时间】:2020-01-09 21:34:54
【问题描述】:
以下是我的代码,我遵循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 响应的自动转换,我想观察它。
【问题讨论】:
标签: android retrofit2 rx-java2 rx-android