依赖
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 create(Class 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 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 GetFirstCategory();
//
@PUT("order/verify/v1/syncShoppingCart")
@FormUrlEncoded
//http://172.17.8.100/small/order/verify/v1/syncShoppingCart
Observable GetShopping(@Header("userId") int userId,
@Header("sessionId") String sessionId,
@Field("data") String data);
使用方式
//Observer中ShoppifBean是数据类
HttpRetrofitUile.get().create(Api.class).GetSHOPPIF("71")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
//@POST请求方法最好加上@FormUrlEncoded使用@FieldMap或者@Field
//@Query方式可能出错
@FormUrlEncoded
@POST
Call
//也可以这样使用
(.subscribe(new Observer
@GET
Observable
)
//还可以使用泛型传递数据
()
各种API
public interface Api {
//查询
@GET("commodity/v1/findCommodityByKeyword")
//http://172.17.8.100/small/commodity/v1/findCommodityByKeyword
Observable
//一级类目
@GET("commodity/v1/findFirstCategory")
//http://172.17.8.100/small/commodity/v1/findFirstCategory
Observable
//同步购物车
@PUT("order/verify/v1/syncShoppingCart")
@FormUrlEncoded
//http://172.17.8.100/small/order/verify/v1/syncShoppingCart
Observable
@Header("sessionId") String sessionId,
@Field("data") String data);
//查询购物车
@GET("order/verify/v1/findShoppingCart")
//http://172.17.8.100/small/order/verify/v1/findShoppingCart
Observable
@Header("sessionId") String sessionId);
//点赞
@POST("circle/verify/v1/addCircleGreat")
@FormUrlEncoded
//http://172.17.8.100/small/circle/verify/v1/addCircleGreat
Observable
@Header("sessionId") String sessionId,
@Field("circleId") int circleId);
//取消点赞
@DELETE("circle/verify/v1/cancelCircleGreat")
//http://172.17.8.100/small/circle/verify/v1/cancelCircleGreat
Observable
@Header("sessionId") String sessionId,
@Query("circleId") int circleId);
//二级类目接口
@GET("commodity/v1/findSecondCategory")
//http://172.17.8.100/small/commodity/v1/findSecondCategory
Observable
//二级查询
@GET("commodity/v1/findCommodityByCategory")
//http://172.17.8.100/small/commodity/v1/findCommodityByCategory
Observable
@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
@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
@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
@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
@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
@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
@Header("sessionId") String sessionId,
@Field("orderInfo") String orderInfo,
@Field("totalPrice") double totalPrice,
@Field("addressId") int addressId
);
}