基于Retrofit2与RxJava2的多接口请求适配问题(指定依赖版本)
适配RxJava2+Retrofit2的多API请求汇总解决方案
已知存在多API请求汇总的相似方案,但现有方案因依赖版本差异无法适配,当前依赖版本如下:
dependencies { implementation 'androidx.appcompat:appcompat:1.5.0' implementation 'com.google.android.material:material:1.6.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' implementation 'com.google.code.gson:gson:2.9.1' implementation "com.squareup.retrofit2:adapter-rxjava2:2.3.0" testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' }
现有接口及多请求处理逻辑:
public interface QuoteService { @GET("https://www.alphavantage.co/query?function=GLOBAL_QUOTE") Call<Quote> getQuote (@Query("symbol") String company, @Query("apikey") String api_key); }
for (String s: companylist) { Call<Quote> call = service.getQuote(s, API.API_KEY); call.enqueue(new Callback<Quote>() { @Override public void onResponse(Call<Quote> call, Response<Quote> response) { if (response.isSuccessful()) { data = response.body(); items.add(data.getApp()); } } }); }
需求:用RxJava2处理多请求,汇总结果为列表后设置适配器,解决subscribe和Consumer的“cannot resolve method”报错。
步骤1:补充RxJava2核心依赖
当前依赖仅包含Retrofit的RxJava2适配器,缺少RxJava2核心库及Android绑定库,这是导致类找不到、方法无法解析的主要原因之一,需添加:
dependencies { // 新增RxJava2核心库 implementation 'io.reactivex.rxjava2:rxjava:2.2.21' // 新增RxJava2 Android绑定库 implementation 'io.reactivex.rxjava2:rxandroid:2.1.1' // 原依赖保留 implementation 'androidx.appcompat:appcompat:1.5.0' implementation 'com.google.android.material:material:1.6.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' implementation 'com.google.code.gson:gson:2.9.1' implementation "com.squareup.retrofit2:adapter-rxjava2:2.3.0" testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' }
步骤2:修改QuoteService接口并配置Retrofit
将接口返回类型从Call<Quote>改为RxJava2的Observable<Quote>,同时确保Retrofit实例添加RxJava2适配器:
import io.reactivex.Observable; import retrofit2.http.GET; import retrofit2.http.Query; public interface QuoteService { // 改为相对路径,基础URL移至Retrofit配置 @GET("query?function=GLOBAL_QUOTE") Observable<Quote> getQuote (@Query("symbol") String company, @Query("apikey") String api_key); }
创建Retrofit实例时必须添加RxJava2CallAdapterFactory:
Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://www.alphavantage.co/") .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); QuoteService service = retrofit.create(QuoteService.class);
步骤3:用RxJava2操作符处理多请求并汇总
使用RxJava2的操作符统一管理多请求,汇总结果后在主线程更新适配器:
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.functions.Consumer; import io.reactivex.schedulers.Schedulers; import java.util.ArrayList; import java.util.List; // 处理多请求逻辑 Observable.fromIterable(companylist) .flatMap(s -> service.getQuote(s, API.API_KEY) // 过滤空响应,避免后续空指针 .filter(quote -> quote != null) // 网络请求放在IO线程 .subscribeOn(Schedulers.io())) .toList() // 自动汇总所有成功请求的结果为List<Quote> .observeOn(AndroidSchedulers.mainThread()) // 切换到主线程更新UI .subscribe(new Consumer<List<Quote>>() { @Override public void accept(List<Quote> quotes) throws Exception { // 转换为你需要的items列表 List<YourAppItem> items = new ArrayList<>(); for (Quote quote : quotes) { items.add(quote.getApp()); } // 在这里设置并更新适配器 yourAdapter.setItems(items); yourAdapter.notifyDataSetChanged(); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { // 处理请求过程中的异常(如网络错误、解析失败) throwable.printStackTrace(); // 可添加Toast或错误提示UI } });
关键注意事项
- 导入正确的Consumer类:必须使用
io.reactivex.functions.Consumer(RxJava2的类),不要导入RxJava3的同名类,这是解决“cannot resolve method”报错的核心。 - 线程切换:
subscribeOn指定网络请求的线程,observeOn指定UI更新的线程,避免阻塞主线程造成ANR。 - 异常处理:务必实现错误回调,避免未捕获的异常导致崩溃。
内容的提问来源于stack exchange,提问作者Nicholas Joshua
相关产品推荐
相关产品推荐

