![](/img/trans.png)
[英]cannot resolve method 'subscribeon(io.reactivex.scheduler)'
[英]Cannot resolve method SubscribeOn - RxJava
我是 RxJava 的新手,我正在嘗試使用 RxJava 進行 Retrofit 調用。 當我在 SubscribeOn 上編寫這段代碼時,它說“無法解析方法 SubscribeOn(io.reactivex.scheduler)”。
你能指導我哪里做錯了嗎?
感謝 R
Presenter層中的getDemoData。
void getDemoData(){
mCompositeDisposable.add(apiInterface.getDemoData()
//subscribeOn has the error.
.subscribeOn(Schedulers.io()) // "work" on io thread
.observeOn(AndroidSchedulers.mainThread()) // "listen" on UIThread
.map(new Function<IApiCalls, List<DemoJSONAPIData>>() {
@Override
public List<DemoJSONAPIData> apply(
@io.reactivex.annotations.NonNull final IApiCalls apiCalls)
throws Exception {
// we want to have the geonames and not the wrapper object
return apiCalls.getDemoData();
}
})
.subscribe(new Consumer<List<Geoname>>() {
@Override
public void accept(
@io.reactivex.annotations.NonNull final List<Geoname> geonames)
throws Exception {
//display
}
})
);
}
Api接口
public interface ApiInterface {
@GET("/posts")
//Single<DemoJSONAPIData> getDemoData();
Call<List<DemoJSONAPIData>> getDemoData();
}
IApi調用
public interface IApiCalls {
List<DemoJSONAPIData> getDemoData();
}
客戶端
public class ApiClient {
public static final String BASE_URL_TWO = "https://jsonplaceholder.typicode.com";
public static Retrofit retrofit = null;
public static Retrofit getApiClient()
{
if(retrofit == null)
{
retrofit = new Retrofit.Builder().baseUrl(BASE_URL_TWO)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
return retrofit;
}
}
依賴關系
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.1.0'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:converter-jackson:2.2.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
編輯
我想使用 RxJava/rxAndroid 實現的 Retrofit 調用
@Override
public List<DemoJSONAPIData> getDemoData() {
try{
demoJSONAPIDatas = new ArrayList<>();
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<List<DemoJSONAPIData>> call = apiInterface.getDemoData();
call.enqueue(new Callback<List<DemoJSONAPIData>>() {
@Override
public void onResponse(Call<List<DemoJSONAPIData>> call, Response<List<DemoJSONAPIData>> response) {
Log.d("MainActivity", "Status Code = " + response.code());
demoJSONAPIDatas = response.body();
Log.d("demoJSONAPIDatas", demoJSONAPIDatas.toString());
for(DemoJSONAPIData demoJSONAPIData: demoJSONAPIDatas){
Log.d("UserId", demoJSONAPIData.getId());
Log.d("Title", demoJSONAPIData.getTitle());
}
}
@Override
public void onFailure(Call<List<DemoJSONAPIData>> call, Throwable t) {
Log.d("error", "error");
}
});
}catch (Exception e){
System.out.println("Error " + e.getMessage());
}
return demoJSONAPIDatas;
}
在 ApiInterface 中檢查您的導入是否可觀察。 它應該是 import io.reactivex.Observable;
這也發生在我身上。 所以當我去檢查我的 APIinterface 時,它已經自動導入了“import android.database.Observable;” 而不是'import io.reactivex.Observable;'。 當你把它改成所有錯誤都消失了。
您可能添加了錯誤的包。
這些錯誤:“導入java.util.*”---“導入android.database.Observable”
其實:
“ 導入 io.reactivex.Observable ”
您可以在界面中進行必要的更改。
在api界面導入以下內容即可
導入 rx.Observable;
你的問題將得到解決
api.test(test)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.unsubscribeOn(Schedulers.io())
.subscribe(new Observer<BaseResponse>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(BaseResponse response) {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
Observable<BaseResponse> test(@Field("test") String isOnline);
從 rx java 導入 Observable。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.