Modify! returning a common observable type

I am in the process of writing code to access my web service from my android client. At that moment, when I use the modification to return the observed type for various calls. At the moment when I have 4 of them (shown below)

@FormUrlEncoded @POST("/app/{pathto}") Observable<List<EntryItem>> getNewsAndComments(@Path("pathto") String pathto, @FieldMap Map<String, String> fieldnames); /** * adding a comment. returns observable * with message entity type * * @param pathto * @param fieldnames * @return */ @FormUrlEncoded @POST("/app/{pathto}") Observable<message> postComment(@Path("pathto") String pathto, @FieldMap Map<String, String> fieldnames); @FormUrlEncoded @POST("/{pathto}") Call<message> regDevice(@Path("pathto") String pathto ,@FieldMap Map<String, String> fieldnames); /** * return a Map of canteen menu items! * * @param path * @param fielditems * @return */ @FormUrlEncoded @POST("/app/{pathto}") Observable<Map<String, ArrayList<CantItem>>> getCanteenItems(@Path("path") String path, @FieldMap Map<String,String> fielditems); 

I also use the generic RestService factory to create a universal retooling service. I stole this from a blog post I read :).

  public static <T> T getRetroFitService(final Class<T> clazz, final String endpoint) { final Retrofit retrofit = new Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .baseUrl(endpoint) .build(); T service = retrofit.create(clazz); return service; } 

In my interface, where I return observable etc. many method calls look the same, so I tried changing this to the next general call

 Observable<List<T>> 

Unfortunately, the modification does not seem to like it. I was wondering if there is a way around this so that I can make interface calls even more universal, for example by passing my own TypeAdapter to RestServiceFactory? Also, I'm relatively new to modernization and rx-android, so any advice is welcome. Thanks!

+5
source share
1 answer

you can use the flatMap function. I did the same in my application.

 OrderController.getOrderInterface() .resendEventSMS(Common.getUserToken(this),order_id) .subscribeOn(Schedulers.io()) .flatMap(result ->Observable.just(new RxSelection(RESEND_SMS,result))) .onErrorReturn(throwable ->new RxSelection(RESEND_SMS_ERROR,getError(throwable))) .observeOn(AndroidSchedulers.mainThread()); .subscribe(mOrderObserver); 

The Rx selection class is a general class:

 public class RxSelection { private int what; private Object obj; public RxSelection(int what, Object obj){ this.obj=obj; this.what=what; } public int getWhat(){ return what; } public Object getObj(){ return obj; } } 

The observer is as follows:

 mOrderObserver = new Observer<RxSelection>() { @Override public void onSubscribe(Disposable d) { Common.log(EventOrderDetailActivity.this,TAG, "subscribe"); mDisposableList.add(d); } @Override public void onNext(RxSelection value) { Common.log(EventOrderDetailActivity.this,TAG, "OnNext"); switch (value.getWhat()) { case GET_EVENT_ORDER_DETAILS: mEventOrderDetail= (EventOrderDetail) value.getObj(); setupData(); break; case RESEND_SMS: HashMap<String,String> sms= (HashMap<String,String>) value.getObj(); Toast.makeText(EventOrderDetailActivity.this, sms.get("success"), Toast.LENGTH_SHORT).show(); break; } 

here you will convert the result of all calls to an RxSelection object. and you need only one observer to get the result of observing any service with any type of result.

+1
source

Source: https://habr.com/ru/post/1241528/


All Articles