retrofit+rxjava封装

时间:2023-03-09 01:56:05
retrofit+rxjava封装
public class RetrofitHelper {
private static OkHttpClient okHttpClient;
private static ServiceAPI serviceAPI;
private static ServiceAPI api;
static {
initOkhttpClient();
}
private static OkHttpClient initOkhttpClient() {
if (okHttpClient==null){
synchronized (RetrofitHelper.class){
if (okHttpClient==null){
okHttpClient=new OkHttpClient.Builder()
.build();
} }
}
return okHttpClient;
}
public static ServiceAPI getAPI(){
if (serviceAPI==null){
synchronized (ServiceAPI.class){
if (serviceAPI==null){
api = RetrofitHelper.createAPI(ServiceAPI.class,Api.URl);
}
}
}
return api;
}
/**
*
* @param clzz
* @param url
* @param <T>
* @return
*/
public static <T> T createAPI(Class<T> clzz,String url){
Retrofit retrofit=new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
return retrofit.create(clzz);
}
}
public interface ServiceAPI {
//首页
@GET(Api.HOME)
Observable<Home_Bean> home();
//分类
@GET(Api.CLASSIFY)
Observable<Classify_Bean>classify();
//分类子列表
@GET(Api.CLASSIFY2)
Observable<ClassifyBean2>classify2(@Query("cid")String cid);
@GET(Api.PRODUCTLIST)
Observable<Productlist_Bean>productlist(@Query("pscid")String pscid);
//详情
@GET(Api.PARTICULARS)
Observable<Particulars_Bean>particulars(@Query("pid")String pid,@Query("suorce")String suorce);
//添加购物车
@GET(Api.ADDCART)
Observable<AddCart_Bean>addcart(@QueryMap Map<String,String>map);
//登录
@GET(Api.LOGIN)
Observable<Login_Bean>login(@Query("mobile")String name,@Query("password")String pass);
//注册
@GET(Api.REGISTER)
Observable<Register_Bean>register(@Query("mobile")String name,@Query("password")String pass);
//订单列表
@GET(Api.ORDERSLIST)
Observable<Indent_Bean>indentlist(@QueryMap Map<String,String>map);
}