【发布时间】:2019-12-16 16:05:16
【问题描述】:
我是 Rx java 的新手,我有一个场景,我想从多个 API 获取数据。这些 API 彼此独立,但我想在视图中显示来自这些 API 的数据。所以我想以这样的方式进行这些 API 调用,以便我可以同时获取每个 API 数据。我已经在使用改造 2。我对 RX JAVA 有点了解,但我只知道如何一次发出一个请求。请帮忙
Retorfit Rest 客户端:
public class RestClient {
private static ApiService apiService = null;
public static ApiService getApiService(String url) {
apiService = null;
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okClient = new OkHttpClient.Builder().
connectTimeout(1, TimeUnit.MINUTES).
readTimeout(3, TimeUnit.MINUTES).
addInterceptor(interceptor).build();
Gson gson = new GsonBuilder().setLenient().create();
Retrofit = new Retrofit.Builder()
.baseUrl(url).client(okClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
apiService = restAdapter.create(ApiService.class);
return apiService;
}
}
API 服务接口:
public interface ApiService {
@GET("v2/nearest_city")
Observable<AqiDto> getAQI(@Query("lat") String latitude,
@Query("lon") String longitude,
@Query("key") String key);
@GET("data/2.5/weather")
Observable<WeatherDTO> getWeather(@Query("lat") String latitude,
@Query("lon") String longitude,
@Query("appid") String id);
}
存储库类:
public class SiteListRepository {
private static final String TAG = "SITE_LIST_REPO";
private final CompositeDisposable disposable;
private Context mContext;
private AppUtilities mAppUtilities;
public SiteListRepository(Application application) {
mContext = application.getApplicationContext();
disposable = new CompositeDisposable();
mAppUtilities = new AppUtilities(mContext);
}
public Object getData() {
disposable = Observable.merge(RestClient.getApiService(BASE_URL_AIR_INDEX).getAQI(EcoHubApplication.mAppSharedPreference.getLatitude(), EcoHubApplication.mAppSharedPreference.getLongitude(), "2664b262-6369-415c-aa5a-ef2bd9ccf1cf")
.subscribeOn(Schedulers.newThread()), RestClient.getApiService(BASE_URL_OPEN_WEATHER).getWeather(EcoHubApplication.mAppSharedPreference.getLatitude(), EcoHubApplication.mAppSharedPreference.getLongitude(), "b6907d289e10d714a6e88b30761fae22")
.subscribeOn(Schedulers.newThread())).observeOn(AndroidSchedulers.mainThread()).subscribe(obj -> {
object = obj;
});
return object;
}
}
想要将这 2 个 API 调用合并在一起。
视图模型:
public class SiteListViewModel extends AndroidViewModel {
private SiteListRepository siteListRepository;
public MutableLiveData<AqiDto> aqiDTOMutableLiveData = new MutableLiveData<>();
public MutableLiveData<WeatherDTO> weatherDTOMutableLiveData = new MutableLiveData<>();
private Object object;
public SiteListViewModel(@NonNull Application application) {
super(application);
siteListRepository = new SiteListRepository(application);
}
public void getData(){
object = siteListRepository.getData();
if (object instanceof AqiDto){
aqiDTOMutableLiveData.setValue((AqiDto) object);
} else if (object instanceof WeatherDTO){
weatherDTOMutableLiveData.setValue((WeatherDTO) object);
}
}
}
【问题讨论】:
-
您可以选择
Single.zip(...)或Single.zipArray(...) -
Zip 并不是最有效的管理方式。我正在寻找合并。
-
然后使用
Single.merge(...)。另外,“不是最有效的方法”是什么意思?根据你的使用情况,zip就能达到你想要的效果。 -
proandroiddev.com/… 阅读,如果有错误请告诉我。
-
文章的哪一部分说 zip 无效?因为在我看来,这篇文章是在赞扬它的能力。
标签: android rx-java rx-java2 rx-android