封装 Retrofit+Rxjava 无需修改添加Service接口中的方法

一般Retrofit中的用法都要去写个Api接口类,然后在类里面对后台接口写上对应的方法例如这样

public interface IScanService {

    @FormUrlEncoded
    @POST("findShopList")
    Observable<BaseResult<List<ScanLocBean>>> findShopList(@Field("shopName") String
                                                                   shopName, @Field
                                                                   ("latitude") String
                                                                   latitude, @Field("longitude")
                                                                   String longitude);

    @POST("scan/getCartInfo")
    Observable<BaseResult<ShopCartListBean>> getCartInfo();

    @FormUrlEncoded
    @POST("scan/addProduct")
    Observable<BaseResult<Object>> addProduct(@Field("productId") String
                                                      productId, @Field
                                                      ("productNum") int productNum);
。。。

但是我这里的封装中这样写就行了

public interface APIInterface {


    @FormUrlEncoded
    @POST
    Observable<Response<ResponseBody>> doPost(@Url String Url, @FieldMap HashMap<String, String> map);

    @Streaming
    @GET
    Call<ResponseBody> doDownload(@Url String Url);
}
 
 

然后这个类就什么都不用动了,其中doPost()是包含了所有的post请求,同理下面的doDownload是下载的请求,后续会加上上传的请求

具体代码见github地址https://github.com/wangxiongtao/Retrofit-RxJava-Mvp



猜你喜欢

转载自blog.csdn.net/w_1220577523/article/details/79291108