确定。所以API文档说:
通过使用count的最大值更改'start'和'count'参数,最多可获得100家餐厅 20。 API链接:https://developers.zomato.com/documentation#!/restaurant/search
这是示例代码:
restaurantList = new ArrayList<>();
restaurantFinderAPI = RetrofitClient.getRetrofitClientAPI();
restaurantScheduler = Schedulers.newThread();
for (int i = 0; i <= 80; i += 20) {
restaurantModelObservable = restaurantFinderAPI.getRestaraunts(city, entitytype, searchString, i, 20);
restaurantModelObservable.subscribeOn(restaurantScheduler);
restaurantModelObservable.observeOn(AndroidSchedulers.mainThread());
restaurantModelObservable.subscribe(new Observer<RestaurantModel>() {
@Override
public void onSubscribe(Disposable d) {
COUNT++;
restaurantDisposable = d;
}
@Override
public void onNext(RestaurantModel value) {
restaurantList.addAll(value.getRestaurants());
}
@Override
public void onError(Throwable e) {
restaurantDisposable.dispose();
onRestaurantSync.onRestaurantSyncFail();
}
@Override
public void onComplete() {
if (COUNT == 5) {
new ModifyTask().execute(restaurantList);
COUNT = 0;
}
}
});
这是Retrofit界面:
String ZOMATO_ROOT_URL = "https://developers.zomato.com/api/v2.1/";
@GET("search")
@Headers({"Accept: application/json","user-key: 258f3e496748d15cb792bc554000d6b6"})
Observable<RestaurantModel> getRestaraunts(@Query("entity_id") int entityID,
@Query("entity_type") String entity_type,
@Query("q")String searchFor,
@Query("start") int start,
@Query("count") int count);
我通过COUNT字段进行5次通话,因此通过更改“i”可以得到100次结果。一旦我得到100个结果,我就开始一个asynctask来对dat进行一些处理。这是多次调用相同API的好方法。我是RXJava的新手,我找不到一个好的答案。请帮忙。感谢
答案 0 :(得分:0)
void scheduleAtFixedRate (TimerTask task,
long delay,
long period)
Timer timer= new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
//Call Service Multiple times
//At EVery 6 seconds
}
},0,6000);
答案 1 :(得分:0)
我是在RX的concat操作员的帮助下完成的。我简单地从像{20,40,60,80}这样的偏移数组中做出了一个观察结果,并将每个发射的调用发送到其余的Api,并且对于每个结果,我将它添加到一个arraylist。
答案 2 :(得分:0)
也许像这样:
Disposable disposable = Observable.range(0, 5)
.subscribeOn(Schedulers.io())
.flatMap(i -> getRestaraunts(city, entitytype, searchString, i * 20, 20))
.observeOn(AndroidSchedulers.mainThread())
.subscribe();