Retrofit简单封装+日志(自用)

依赖

implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
implementation 'io.reactivex.rxjava2:rxjava:2.1.16'
 implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
 implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'

public class HttpRetrofitUile {

    private final Retrofit retrofit;
    private RetrofitApi api = new RetrofitApi();
    //Log日志
    private HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);

     //Ok的拦截器
    private OkHttpClient okHttpClient(){
        return new OkHttpClient.Builder()
                .writeTimeout(5000,TimeUnit.MILLISECONDS)
                .connectTimeout(5000,TimeUnit.MILLISECONDS)
                .readTimeout(5000,TimeUnit.MILLISECONDS)
                .addInterceptor(loggingInterceptor)
                .build();
    }
        //Retrofit的使用
    public HttpRetrofitUile(){
        retrofit = new Retrofit.Builder()
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(api.URLTE)
                .client(okHttpClient())
                .build();
    }
        //可以写成
//
//public static HttpRetrofitUtil get(){
//    if (retrofitUtil==null){
//       synchronized (HttpRetrofitUtil.class){
//            if (retrofitUtil==null){
//                retrofitUtil = new HttpRetrofitUtil();
//            }
//        }
//    }
//    return retrofitUtil;
// }
    public static HttpRetrofitUile get(){
        return Holder.retrofitUile;
    }

    private static class Holder {
        private static final HttpRetrofitUile retrofitUile = new HttpRetrofitUile();
    }
    public <T>T create(Class<T> clazz){
        return retrofit.create(clazz);
    }
}
创建接口类
public class RetrofitApi {
    public String URLTE = "http://www.zhaoapi.cn/product/";
}
创建Api
//@Query是拼参@Header是请求头
 //查询
    @GET("commodity/v1/findCommodityByKeyword")
    //http://172.17.8.100/small/commodity/v1/findCommodityByKeyword
    Observable<KeyWordBean> GetLiSTENER(@Query("keyword") String keyword, @Query("page") int page, @Query("count") int count);

    //
    @GET("commodity/v1/findFirstCategory")
    //http://172.17.8.100/small/commodity/v1/findFirstCategory
    Observable<FirstCategoryBean> GetFirstCategory();

    //
    @PUT("order/verify/v1/syncShoppingCart")
    @FormUrlEncoded
    //http://172.17.8.100/small/order/verify/v1/syncShoppingCart
    Observable<ShoppingCarBean> GetShopping(@Header("userId") int userId,
                                            @Header("sessionId") String sessionId,
                                            @Field("data") String data);
 使用方式
//Observer<ShoppifBean>中ShoppifBean是数据类
HttpRetrofitUile.get().create(Api.class).GetSHOPPIF("71")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ShoppifBean>() {

//@POST请求方法最好加上@FormUrlEncoded使用@FieldMap或者@Field

//@Query方式可能出错

@FormUrlEncoded
    @POST
    Call<String> postMethod(@Url String url, @FieldMap Map<String,String> map);

 

//也可以这样使用

(.subscribe(new Observer<ResponseBody>()

@GET

Observable<ResponseBody> baidu(@Url String url);

//还可以使用泛型传递数据<T>

()

 

 

各种API

public interface Api {
    //查询
    @GET("commodity/v1/findCommodityByKeyword")
    //http://172.17.8.100/small/commodity/v1/findCommodityByKeyword
    Observable<KeyWordBean> GetLiSTENER(@Query("keyword") String keyword, @Query("page") int page, @Query("count") int count);

    //一级类目
    @GET("commodity/v1/findFirstCategory")
    //http://172.17.8.100/small/commodity/v1/findFirstCategory
    Observable<FirstCategoryBean> GetFirstCategory();

    //同步购物车
    @PUT("order/verify/v1/syncShoppingCart")
    @FormUrlEncoded
    //http://172.17.8.100/small/order/verify/v1/syncShoppingCart
    Observable<ShoppingCarBean> GetShopping(@Header("userId") int userId,
                                            @Header("sessionId") String sessionId,
                                            @Field("data") String data);

    //查询购物车
    @GET("order/verify/v1/findShoppingCart")
    //http://172.17.8.100/small/order/verify/v1/findShoppingCart
    Observable<ShoppingBean> GetShoppingCar(@Header("userId") int userId,
                                            @Header("sessionId") String sessionId);

    //点赞
    @POST("circle/verify/v1/addCircleGreat")
    @FormUrlEncoded
    //http://172.17.8.100/small/circle/verify/v1/addCircleGreat
    Observable<DianzanBean> GetDianZan(@Header("userId") int userId,
                                       @Header("sessionId") String sessionId,
                                       @Field("circleId") int circleId);

    //取消点赞
    @DELETE("circle/verify/v1/cancelCircleGreat")
//http://172.17.8.100/small/circle/verify/v1/cancelCircleGreat
    Observable<DianzanBean> GetReset(@Header("userId") int userId,
                                     @Header("sessionId") String sessionId,
                                     @Query("circleId") int circleId);

    //二级类目接口
    @GET("commodity/v1/findSecondCategory")
    //http://172.17.8.100/small/commodity/v1/findSecondCategory
    Observable<SecondCategoryBean> GetSecondCategory(@Query("firstCategoryId") String firstCategoryId);

    //二级查询
    @GET("commodity/v1/findCommodityByCategory")
    //http://172.17.8.100/small/commodity/v1/findCommodityByCategory
    Observable<SecondqueryBean> GetSecondquery(@Query("categoryId") String categoryId,
                                               @Query("page") int page,
                                               @Query("count") int count);

    //修改地址
    @PUT("user/verify/v1/changeReceiveAddress")
    @FormUrlEncoded
    //http://172.17.8.100/small/user/verify/v1/changeReceiveAddress
    Observable<UpdateBean> GetUpdate(@Header("userId") int userId,
                                     @Header("sessionId") String sessionId,
                                     @Field("id") int id,
                                     @Field("realName") String realName,
                                     @Field("phone") String phone,
                                     @Field("address") String address,
                                     @Field("zipCode") String zipCode);

    @PUT("user/verify/v1/modifyUserPwd")
    @FormUrlEncoded
        //http://172.17.8.100/small/user/verify/v1/modifyUserPwd
    Observable<UpdateBean> GetPwdUpdate(@Header("userId") int userId,
                                        @Header("sessionId") String sessionId,
                                        @Field("oldPwd") String oldPwd,
                                        @Field("newPwd") String newPwd);

    @DELETE("circle/verify/v1/deleteCircle")
        //http://172.17.8.100/small/circle/verify/v1/deleteCircle
    Observable<UpdateBean> GetDelete(@Header("userId") int userId,
                                     @Header("sessionId") String sessionId,
                                     @Query("circleId") int circleId);

    @POST("user/verify/v1/setDefaultReceiveAddress")
    @FormUrlEncoded
//http://172.17.8.100/small/user/verify/v1/setDefaultReceiveAddress
    Observable<UpdateBean> GetDefalut(@Header("userId") int userId,
                                      @Header("sessionId") String sessionId,
                                      @Field("id") int id
    );

    @PUT("user/verify/v1/modifyUserNick")
    @FormUrlEncoded
        //http://172.17.8.100/small/user/verify/v1/modifyUserNick
    Observable<UpdateBean> GetName(@Header("userId") int userId,
                                   @Header("sessionId") String sessionId,
                                   @Field("nickName") String nickName);

    //创建订单
    @POST("order/verify/v1/createOrder")
    // //http://172.17.8.100/small/order/verify/v1/createOrder
    @FormUrlEncoded
    Observable<UpdateBean> GetcreateOrder(@Header("userId") int userId,
                                          @Header("sessionId") String sessionId,
                                          @Field("orderInfo") String orderInfo,
                                          @Field("totalPrice") double totalPrice,
                                          @Field("addressId") int addressId
    );

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值