工具类
public class RetrofitUtil {
private static RetrofitUtil retrofitUtil;
private static APIFunction apiFunction;
private RetrofitUtil() {
OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
.connectTimeout(5000, TimeUnit.SECONDS)
.readTimeout(5000, TimeUnit.SECONDS)
.writeTimeout(5000, TimeUnit.SECONDS)
//添加日志拦截器
// .addInterceptor(InterceptorUtil.HeaderInterceptor())
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.build();
Retrofit mRetrofit = new Retrofit.Builder()
.baseUrl(BaseAPI.GETAD)
.addConverterFactory(GsonConverterFactory.create())//添加gson转换器
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())//添加rxjava转换器
.client(mOkHttpClient)
.build();
apiFunction = mRetrofit.create(APIFunction.class);
}
public static RetrofitUtil getInstence() {
if (retrofitUtil == null) {
synchronized (RetrofitUtil.class) {
if (retrofitUtil == null)
retrofitUtil = new RetrofitUtil();
}
}
return retrofitUtil;
}
public APIFunction API() {
return apiFunction;
}
/**
* post请求上传文件
* 参数1 url
* 参数2 回调Callback
*/
public static void upLoadFile(String path, Map<String, Object> params, Callback callback) {
//创建OkHttpClient请求对象
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(5000, TimeUnit.SECONDS)
.readTimeout(5000, TimeUnit.SECONDS)
.writeTimeout(5000, TimeUnit.SECONDS)
.build();
MultipartBody.Builder builder = new MultipartBody.Builder();
//设设置类型 以表单的形式提交
builder.setType(MultipartBody.FORM);
for (Map.Entry<String, Object> entry : params.entrySet()) {
Object object = entry.getValue();
if (!(object instanceof File)) {
builder.addFormDataPart(entry.getKey(), object.toString());
} else {
File file = (File) object;
builder.addFormDataPart(entry.getKey(), file.getName().trim(),
RequestBody.create(MediaType.parse("img/png"), file));
//img/png -> application/octet-stream
}
}
Request request = new Request.Builder()
.post(builder.build())
.url(path)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
}
api网络接口
public class BaseAPI {
public static final String GETAD = "https://www.zhaoapi.cn/";
}
public interface APIFunction {
/**
* 分类
*
* @return
*/
@GET("product/getCatagory")
Observable<ClassifyBean> getCatagory();
/**
* 子分类
*
* @return
*/
@GET("product/getProductCatagory")
Observable<ClassifyChildBean> getProductCatagory(@Query("cid") int cid);
/**
* 查询购物车
*
* @param uid
* @return
*/
@GET("product/getCarts")
Observable<Selectshop> selectCart(@Query("uid") String uid);
}
调用
RetrofitUtil.getInstence().API().getCatagory()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ClassifyBean>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ClassifyBean classifyBean) {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});