5
votes

I am in the process of writing code to access my Webservice from my android client. At the minute I am using retrofit to return an observable type for the various calls.At the moment I have 4 of these (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 a generic RestService factory to create the generic retrofit service. I stole this from a blog post I was reading :) .

 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 of an observable etc a lot of the method calls look the same so I tried changing this to the following generic call

Observable<List<T>>

Unfortunately, retrofit doesn't seem to like this. I was wondering is there any way around this so I can make the interface calls even more generic for example passing in my own TypeAdapter to the RestServiceFactory? Also, I am relatively new to retrofit and rx-android so any advice is appreciated. Thanks!

1

1 Answers

1
votes

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

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); 

Rx selection class is a generic 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;
    }
}

Observer goes like this :

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 are converting the result of all the calls to RxSelection object .and you only require 1 observer to observer result of any service with any type of result