一、定义IHttpEngine规则类
public interface IHttpEngine {
// get 提交
void get(Context context, String url, Map<String, Object> params, EngineCallBack httpCallBack);
// post 提交
void post(Context context, String url, Map<String, Object> params, EngineCallBack httpCallBack);
// 取消请求
// 下载文件
// 上传文件
// https添加安全证书
}
二、定义统一回调EngineCallBack
public interface EngineCallBack {
//开始执行回调,请求网络前的方法
void onPreExecute(Context context, Map<String, Object> params);
//失败
void onError(Exception e);
//成功
void onSuccess(String result);
public final EngineCallBack DEFAULT_CALL_BACK=new EngineCallBack() {
@Override
public void onPreExecute(Context context, Map<String, Object> params) {
}
@Override
public void onError(Exception e) {
}
@Override
public void onSuccess(String result) {
}
};
}
三、实现规则
public class OkHttpEngine implements IHttpEngine {
private static OkHttpClient mOkHttpClient = new OkHttpClient();
private static Handler mHandler = new Handler();
@Override
public void get(Context context,String url, Map<String, Object> params, final EngineCallBack httpCallBack) {
//写请求逻辑
}
@Override
public void post(Context context,String url, Map<String, Object> params, final EngineCallBack httpCallBack) {
// final String jointUrl = HttpUtils.jointParams(url, params); //打印参数
// Log.e("Post请求路径:", jointUrl);
//
// // 了解 Okhhtp
// RequestBody requestBody = appendBody(params);
// Request request = new Request.Builder()
// .url(url)
// .tag(context)
// .post(requestBody)
// .build();
//
// mOkHttpClient.newCall(request).enqueue(
// new Callback() {
// @Override
// public void onFailure(Call call, IOException e) {
// httpCallBack.onError(e);
// }
//
// @Override
// public void onResponse(Call call, okhttp3.Response response) throws IOException {
// // 这个 两个回掉方法都不是在主线程中
// String result = response.body().toString();
// Log.e("Post返回结果:", jointUrl);
// httpCallBack.onSuccess(result);
// }
//
// }
// );
}
}
四、实现回调
public abstract class OkhttpCallBack<T> implements EngineCallBack {
@Override
public void onPreExecute(Context context, Map<String, Object> params) {
//添加公用参数
params.put("", "");
onPreExecute();
}
//开始执行
public void onPreExecute() {
}
@Override
public void onSuccess(String result) {
LogUtil.e("TAG",result);
Gson gson = new Gson();
//转换泛型
T ObjResult = (T) gson.fromJson(result, HttpUtils.analysisClassInfo(this));
onSuccess(ObjResult);
}
public abstract void onSuccess(T result);
}
五、链式调用
public class HttpUtils {
//直接带参数,链式调用
private String mUrl;
// 请求参数
private Map<String, Object> mParams;
//请求方式
private int mType=GET_REQUEST;
// get请求标识
private static final int GET_REQUEST = 0x0011;
// post请求标识
private static final int POST_REQUEST = 0x0022;
// 上下文
private Context mContext;
private HttpUtils(Context context) {
this.mContext = context;
mParams = new HashMap<>();
}
public static HttpUtils with(Context context){
return new HttpUtils(context);
}
//请求方式
public HttpUtils post(){
mType=POST_REQUEST;
return this;
}
public HttpUtils get(){
mType=GET_REQUEST;
return this;
}
//添加参数
public HttpUtils addParams(String key,Object value){
mParams.put(key,value);
return this;
}
public HttpUtils addParams(Map<String,Object> parmas){
mParams.putAll(parmas);
return this;
}
//添加路径
public HttpUtils url(String url){
mUrl=url;
return this;
}
//执行回调
public void execute(EngineCallBack callBack){
callBack.onPreExecute(mContext,mParams);
if (callBack==null){
callBack=EngineCallBack.DEFAULT_CALL_BACK;
}
//判断执行
if (mType==POST_REQUEST){
post(mUrl,mParams,callBack);
}
if (mType==GET_REQUEST){
get(mUrl, mParams, callBack);
}
}
public void execute(){
execute(null);
}
//默认OkHttpEngine
private static IHttpEngine mIHttpEngine=new OkHttpEngine();
//在Application初始化引擎
public static void init(IHttpEngine iHttpEngine){
mIHttpEngine=iHttpEngine;
}
//可以自带引擎
public HttpUtils exchangEngine(IHttpEngine iHttpEngine){
mIHttpEngine=iHttpEngine;
return this;
}
private void get(String url, Map<String, Object> params, EngineCallBack httpCallBack) {
mIHttpEngine.get(mContext,url,params,httpCallBack);
}
private void post(String url, Map<String, Object> params, EngineCallBack httpCallBack) {
mIHttpEngine.post(mContext, url, params, httpCallBack);
}
public static Class<?> analysisClassInfo(Object object){
Type genType = object.getClass().getGenericSuperclass();
Type[] typeArguments = ((ParameterizedType) genType).getActualTypeArguments();
return (Class<?>)typeArguments[0];
}
}
六、使用
HttpUtils.with(context)
.exchangeEngine(new RetrofitEngine());// 切换成Retrofit引擎
.url(UrlConstant.CITY_MAIN_PAGE) // 路径
.addParam("city", mCurrentCity) // 添加参数
.cache(true) // 需要读缓存
.post() // post提交
.execute(new HttpCallBack<CityFragmentEntity>() {
@Override
public void onError(Exception e) {
// 错误回调
}
@Override
public void onSucceed(CityFragmentEntity result) {
// result 返回的解析好的对象 可以直接操作不需要再Json解析
// 成功的回调
}
});
更换框架实现IHttpEngine 接口回调