搜索
ApiService service = RetriftUtils.getinstance().getApiService(Api.url, ApiService.class);
Call<GoodsBean> call = service.getgoods(s, 1, 20);
call.enqueue(new Callback<GoodsBean>() {
@Override
public void onResponse(Call<GoodsBean> call, Response<GoodsBean> response) {
GoodsBean bean = response.body();
callBack.oncall(bean);
}
@Override
public void onFailure(Call<GoodsBean> call, Throwable t) {
}
});
//搜索
// http://172.17.8.100/small/commodity/v1/findCommodityByKeyword
public static final String url="http://172.17.8.100/small/";
// http://172.17.8.100/small/ commodity/v1/findCommodityDetailsById
// commodityId
// 商品详情
// 购物车
// http://172.17.8.100/ks/product/getCarts?uid=51
public static final String gwcurl="http://172.17.8.100/ks/";
//搜索
// commodity/v1/findCommodityByKeyword
@GET("commodity/v1/findCommodityByKeyword")
Call<GoodsBean> getgoods(@Query("keyword")String keyword,@Query("page")int page,@Query("count")int count);
//商品详情
// commodity/v1/findCommodityDetailsById
@GET("commodity/v1/findCommodityDetailsById")
Call<GoodsXqBean> getxq(@Query("commodityId")int commodityId);
// product/getCarts?uid=51
@GET("product/getCarts?uid=51")
Call<GwcBean> gwc();
private static RetriftUtils retriftUtils;
private RetriftUtils() {
}
public static RetriftUtils getinstance() {
if (retriftUtils == null) {
synchronized (RetriftUtils.class) {
if (retriftUtils == null) {
return retriftUtils = new RetriftUtils();
}
}
}
return retriftUtils;
}
public static Retrofit getRetrift(String url) {
//请求头
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder()
.addHeader("userid", "")
.addHeader("sessionid", "")
.build();
return chain.proceed(request);
}
});
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
public <T> T getApiService(String url, Class<T> service) {
Retrofit retrofit = getRetrift(url);
//通过java动态代理模式获取代理对象
T t = retrofit.create(service);
return t;
}