The goal is to make two types of requests using the REST library in an Android application, get two types of objects, and fill one with another.
You can, of course, gash asink task and consistently perform all operations. And you can try to use RXjava, but I’m familiar with it the second day, and I still don’t understand much, not to mention lambda expressions, articles on Habré are also far gone, almost nothing is clear.
Karoch. There are two classes 1. offerClass - each class when sparring json gets its id. contains an empty orderStatus class that can be retrieved by another request using id. 2. orderStatus - the class contains information for a specific offerClass id.
There is a variable offers = new ArrayList (); which when onCreate () is zero, and is given to the adapter to build the View.
In the onCreate () method, the downloadData () function is called which, using RXjava, downloads and fills the data, and returns it to the adapter in the main stream.
What I want to do:
1.выполнить асинхронный запрос getActiveSimpleOrder(1) 2.по нему получить List<offerClass> и присвоить его offers. 3. наполнить каждый объект offers (offer - статутом, зная id) for(offerClass offer:offers) // для каждого объекта offers { 4. getOrderStatus(offer.getId()) //Получив статус для конкретного объекта. и далее offer.setStatus(response.body()) // Наполняем объект статусом. } 5. когда все выполнится передать offers в основной поток и вызвать 6. adapter.notifyDataSetChanged(). After the answers a new code was generated:
In the main fragment method:
public void onStart() { initializeData(); // Получаем данные } Function code:
private void initializeData() { int rest_id = 1; ApiInterface api = ApiModule.getClient(); api.getActiveSimpleOrder(rest_id) .subscribeOn(Schedulers.io()) .flatMap(Observable::from) .flatMap(offer -> api.getStatus(offer.getId()) .doOnNext(offer::setStatus) ******* .map(status -> offer) ******************* ) .toList() .observeOn(AndroidSchedulers.mainThread()) .subscribe(adapter::setOffers); } Api code:
public class ApiModule { private static ApiInterface apiInterface; private static String baseUrl = ""http://85.143.221.39/EatgidServer/"; public static ApiInterface getClient() { if (apiInterface == null) { Retrofit client = new Retrofit.Builder() .baseUrl(baseUrl) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); apiInterface = client.create(ApiInterface.class); } return apiInterface; } } The string with asterisks is underlined in red: the compiler compiles anyway. which is strange. varning is as follows: 
The application works with the error:
LogCat:
06-13 19:59:53.954 13429-13429/eatgid.com.restaurant E/AndroidRuntime: FATAL EXCEPTION: main Process: eatgid.com.restaurant, PID: 13429 java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add `onError` handling. at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:60) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5631) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) Caused by: rx.exceptions.OnErrorNotImplementedException: 1465819531027 at rx.Observable$27.onError(Observable.java:8139) at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:157) at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:120) at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:254) at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:186) at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5631) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) Caused by: com.google.gson.JsonSyntaxException: 1465819531027 at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:74) at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:59) at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:41) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:116) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:216) at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37) at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25) at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:116) at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:211) at retrofit2.OkHttpCall.execute(OkHttpCall.java:174) at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(RxJavaCallAdapterFactory.java:160) at rx.Subscriber.setProducer(Subscriber.java:211) at rx.Subscriber.setProducer(Subscriber.java:205) at rx.Subscriber.setProducer(Subscriber.java:205) at rx.Subscriber.setProducer(Subscriber.java:205) at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:141) at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:127) at rx.Observable$2.call(Observable.java:162) at rx.Observable$2.call(Observable.java:154) at rx.Observable$2.call(Observable.java:162) at rx.Observable$2.call(Observable.java:154) at rx.Observable$2.call(Observable.java:162) at rx.Observable$2.call(Observable.java:154) at rx.Observable.unsafeSubscribe(Observable.java:8314) at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext(OperatorMerge.java:235) at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext(OperatorMerge.java:145) at rx.internal.operators.OperatorMap$1.onNext(OperatorMap.java:54) at rx.internal.operators.OperatorMerge$MergeSubscriber.emitScalar(OperatorMerge.java:368) at rx.internal.operators.OperatorMerge$MergeSubscriber.tryEmit(OperatorMerge.java:330) at rx.internal.operators.OperatorMerge$InnerSubscriber.onNext(OperatorMerge.java:807) at rx.internal.operators.OnSubscribeFromIterable$IterableProducer.slowpath(OnSubscribeFromIterable.java:97) at rx.internal.operators.OnSubscribeFromIterable$IterableProducer.request(OnSubscribeFromIterable.java:73) at rx.Subscriber.setProducer(Subscriber.java:211) at rx.internal.operators.OnSubscribeFromIterable.call(OnSubscribeFromIterable.java:49) at rx.internal.operators.OnSubscribeFromIterable.call(OnSubscribeFromIterable.java:32) at rx.Observable.unsafeSubscribe(Observable.java:8314) at rx.internal.op
HttpException: HTTP 400 Bad Request- xkor.subscribe(adapter::setOffers, throwable -> throwable.printStackTrace());. I am writing from memory so that the name of the printStackTrace method could not be written correctly. - xkor