我正在使用多个api调用从服务器中获取数据,因为我正在使用Retrofit @和RxJava2。在我的api服务类中,我有2个get请求,并且由于数据不完全相同,它们都分别运行。希望同时从这两个api获取所有数据。目前,我正在获取类似的数据,并且希望有更好的方法来处理多个api调用。
下面是我的代码:
ApiService.class
public interface ApiService {
@GET("Categoery_api")
Observable<List<MenuModel>> getData();
@GET("Recent_six_post_api")
Observable<List<FlashStoryModel>> getFlashStory();
}
MainActivity.java
private void getMenu(){
Retrofit retrofit = RetrofitClient.getInstance();
ApiService myApi = retrofit.create(ApiService.class);
myApi.getData().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<MenuModel>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(List<MenuModel> menuModels) {
if(menuModels.size() > 0){
menuProg.setVisibility(View.INVISIBLE);
list.addAll(menuModels);
adapter = new MenuAdapter(list,getApplicationContext());
menuRecycler.setAdapter(adapter);
}
}
@Override
public void onError(Throwable e) {
menuProg.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
}
@Override
public void onComplete() {
}
});
}
private void getFlashStory(){
Retrofit retrofit = RetrofitClient.getInstance();
ApiService apiService = retrofit.create(ApiService.class);
apiService.getFlashStory().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<FlashStoryModel>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(List<FlashStoryModel> flashStoryModels) {
if(flashStoryModels.size() > 0){
prog1.setVisibility(View.INVISIBLE);
storyList.addAll(flashStoryModels);
flashStoryAdapter = new FlashStoryAdapter(getActivity(),storyList);
flashStory.setAdapter(flashStoryAdapter);
final Handler handler = new Handler();
final Runnable update = new Runnable() {
@Override
public void run() {
if (currentPage == 6) {
currentPage = 0;
}
flashStory.setCurrentItem(currentPage++, true);
}
};
timer = new Timer(); // This will create a new Thread
timer.schedule(new TimerTask() { // task to be scheduled
@Override
public void run() {
handler.post(update);
}
}, DELAY_MS, PERIOD_MS);
}
}
@Override
public void onError(Throwable e) {
prog1.setVisibility(View.INVISIBLE);
Toast.makeText(getActivity(),e.getMessage(),Toast.LENGTH_SHORT).show();
}
@Override
public void onComplete() {
}
});
}
请让我知道如何达到预期效果。任何帮助将不胜感激。
谢谢
答案 0 :(得分:1)
使用Observable.zip(...)
Observable.zip(
apiService.getFlashStory(),
apiService.getData(),
BiFunction< List<FlashStoryModel>, List<MenuModel>, Unit> { r1, r2, _ ->
// r1 is result from apiService.getFlashStory()
// r2 is result from apiService.getData()
// Do something here.
// For example, this is what your code do above for apiService.getData()
if(r2.size() > 0){
menuProg.setVisibility(View.INVISIBLE);
list.addAll(r2);
adapter = new MenuAdapter(list,getApplicationContext());
menuRecycler.setAdapter(adapter);
}
}
).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
这是实际的BiFunction。
object: BiFunction<P, Q, R> {
override fun apply(t1: P, t2: Q): R {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
但是我写成。
BiFunction<P, Q, R> { r1, r2, returnValue ->
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
答案 1 :(得分:0)
您可以同时调用,但要启动新线程,请勿为此目的使用主线程。
myApi.getData().subscribeOn(Schedulers.newThread()) .observeOn(Schedulers.newThread())
这样,您可以避免应用程序挂起或滞后。