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* @return */ public static T createAPI(Class 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) Observablehome();//分类 @GET(Api.CLASSIFY) Observable classify(); //分类子列表 @GET(Api.CLASSIFY2) Observable classify2(@Query("cid")String cid); @GET(Api.PRODUCTLIST) Observable productlist(@Query("pscid")String pscid); //详情 @GET(Api.PARTICULARS) Observable particulars(@Query("pid")String pid,@Query("suorce")String suorce); //添加购物车 @GET(Api.ADDCART) Observable addcart(@QueryMap Map map); //登录 @GET(Api.LOGIN) Observable login(@Query("mobile")String name,@Query("password")String pass); //注册 @GET(Api.REGISTER) Observable register(@Query("mobile")String name,@Query("password")String pass); //订单列表 @GET(Api.ORDERSLIST) Observable indentlist(@QueryMap Map map);}