标签:
之前项目中一直使用的Xutils开源框架,从xutils 2.1.5版本使用到最近的xutils 3.0,使用起来也是蛮方便的,只不过最近想着完善一下app中使用的开源框架,由于Xutils里面包含的东西相对来说比较杂,有数据库、图片缓存、注解、网络请求等等,秉着一个开源库只处理一件事的想法,决定逐步替换到Xutils,上网搜了一下比较好的开源框架,就找到了okHttp、volley、android-async-http等比较推荐的开源网络请求,该如何选择呢?
通过上面的对比说明,让你不得不做出明智的选择,OkHttp是一个相对成熟的解决方案,据说Android4.4的源码中可以看到HttpURLConnection已经替换成OkHttp实现了,所以决定选择采用okhttp。
官网地址:http://square.github.io/okhttp/
官方API地址:http://m.blog.csdn.net/article/details?id=50747352
github源码地址:https://github.com/square/okhttp
1.)OkHttpClient.java
2.)Request.java
3.)Call.java
4.)RequestBody.java
5.)Response.java
compile ‘com.squareup.okhttp3:okhttp:3.2.0‘
RequestManager.java 全局属性解说
private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");//mdiatype 这个需要和服务端保持一致 private static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown; charset=utf-8");//mdiatype 这个需要和服务端保持一致 private static final String TAG = RequestManager.class.getSimpleName(); private static final String BASE_URL = "http://xxx.com/openapi";//请求接口根地址 private static volatile RequestManager mInstance;//单利引用 public static final int TYPE_GET = 0;//get请求 public static final int TYPE_POST_JSON = 1;//post请求参数为json public static final int TYPE_POST_FORM = 2;//post请求参数为表单 private OkHttpClient mOkHttpClient;//okHttpClient 实例 private Handler okHttpHandler;//全局处理子线程和M主线程通信
RequestManager.java 构造函数
/** * 初始化RequestManager */ public RequestManager(Context context) { //初始化OkHttpClient mOkHttpClient = new OkHttpClient().newBuilder() .connectTimeout(10, TimeUnit.SECONDS)//设置超时时间 .readTimeout(10, TimeUnit.SECONDS)//设置读取超时时间 .writeTimeout(10, TimeUnit.SECONDS)//设置写入超时时间 .build(); //初始化Handler okHttpHandler = new Handler(context.getMainLooper()); }
RequestManager.java 获取单利引用 这里用到了双重检查锁实现单例
/** * 获取单例引用 * * @return */ public static RequestManager getInstance(Context context) { RequestManager inst = mInstance; if (inst == null) { synchronized (RequestManager.class) { inst = mInstance; if (inst == null) { inst = new RequestManager(context.getApplicationContext()); mInstance = inst; } } } return inst; }
同步请求统一入口
/** * okHttp同步请求统一入口 * @param actionUrl 接口地址 * @param requestType 请求类型 * @param paramsMap 请求参数 */ public void requestSyn(String actionUrl, int requestType, HashMap<String, String> paramsMap) { switch (requestType) { case TYPE_GET: requestGetBySyn(actionUrl, paramsMap); break; case TYPE_POST_JSON: requestPostBySyn(actionUrl, paramsMap); break; case TYPE_POST_FORM: requestPostBySynWithForm(actionUrl, paramsMap); break; } }
okHttp get同步请求
/** * okHttp get同步请求 * @param actionUrl 接口地址 * @param paramsMap 请求参数 */ private void requestGetBySyn(String actionUrl, HashMap<String, String> paramsMap) { StringBuilder tempParams = new StringBuilder(); try { //处理参数 int pos = 0; for (String key : paramsMap.keySet()) { if (pos > 0) { tempParams.append("&"); } //对参数进行URLEncoder tempParams.append(String.format("%s=%s", key, URLEncoder.encode(paramsMap.get(key), "utf-8"))); pos++; } //补全请求地址 String requestUrl = String.format("%s/%s?%s", BASE_URL, actionUrl, tempParams.toString()); //创建一个请求 Request request = addHeaders().url(requestUrl).build(); //创建一个Call final Call call = mOkHttpClient.newCall(request); //执行请求 final Response response = call.execute(); response.body().string(); } catch (Exception e) { Log.e(TAG, e.toString()); } }
okHttp post同步请求
/** * okHttp post同步请求 * @param actionUrl 接口地址 * @param paramsMap 请求参数 */ private void requestPostBySyn(String actionUrl, HashMap<String, String> paramsMap) { try { //处理参数 StringBuilder tempParams = new StringBuilder(); int pos = 0; for (String key : paramsMap.keySet()) { if (pos > 0) { tempParams.append("&"); } tempParams.append(String.format("%s=%s", key, URLEncoder.encode(paramsMap.get(key), "utf-8"))); pos++; } //补全请求地址 String requestUrl = String.format("%s/%s", BASE_URL, actionUrl); //生成参数 String params = tempParams.toString(); //创建一个请求实体对象 RequestBody RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, params); //创建一个请求 final Request request = addHeaders().url(requestUrl).post(body).build(); //创建一个Call final Call call = mOkHttpClient.newCall(request); //执行请求 Response response = call.execute(); //请求执行成功 if (response.isSuccessful()) { //获取返回数据 可以是String,bytes ,byteStream Log.e(TAG, "response ----->" + response.body().string()); } } catch (Exception e) { Log.e(TAG, e.toString()); } }
okHttp post同步请求表单提交
/** * okHttp post同步请求表单提交 * @param actionUrl 接口地址 * @param paramsMap 请求参数 */ private void requestPostBySynWithForm(String actionUrl, HashMap<String, String> paramsMap) { try { //创建一个FormBody.Builder FormBody.Builder builder = new FormBody.Builder(); for (String key : paramsMap.keySet()) { //追加表单信息 builder.add(key, paramsMap.get(key)); } //生成表单实体对象 RequestBody formBody = builder.build(); //补全请求地址 String requestUrl = String.format("%s/%s", BASE_URL, actionUrl); //创建一个请求 final Request request = addHeaders().url(requestUrl).post(formBody).build(); //创建一个Call final Call call = mOkHttpClient.newCall(request); //执行请求 Response response = call.execute(); if (response.isSuccessful()) { Log.e(TAG, "response ----->" + response.body().string()); } } catch (Exception e) { Log.e(TAG, e.toString()); } }
异步请求统一入口
/** * okHttp异步请求统一入口 * @param actionUrl 接口地址 * @param requestType 请求类型 * @param paramsMap 请求参数 * @param callBack 请求返回数据回调 * @param <T> 数据泛型 **/ public <T> Call requestAsyn(String actionUrl, int requestType, HashMap<String, String> paramsMap, ReqCallBack<T> callBack) { Call call = null; switch (requestType) { case TYPE_GET: call = requestGetByAsyn(actionUrl, paramsMap, callBack); break; case TYPE_POST_JSON: call = requestPostByAsyn(actionUrl, paramsMap, callBack); break; case TYPE_POST_FORM: call = requestPostByAsynWithForm(actionUrl, paramsMap, callBack); break; } return call; }
okHttp get异步请求
/** * okHttp get异步请求 * @param actionUrl 接口地址 * @param paramsMap 请求参数 * @param callBack 请求返回数据回调 * @param <T> 数据泛型 * @return */ private <T> Call requestGetByAsyn(String actionUrl, HashMap<String, String> paramsMap, final ReqCallBack<T> callBack) { StringBuilder tempParams = new StringBuilder(); try { int pos = 0; for (String key : paramsMap.keySet()) { if (pos > 0) { tempParams.append("&"); } tempParams.append(String.format("%s=%s", key, URLEncoder.encode(paramsMap.get(key), "utf-8"))); pos++; } String requestUrl = String.format("%s/%s?%s", BASE_URL, actionUrl, tempParams.toString()); final Request request = addHeaders().url(requestUrl).build(); final Call call = mOkHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { failedCallBack("访问失败", callBack); Log.e(TAG, e.toString()); } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()) { String string = response.body().string(); Log.e(TAG, "response ----->" + string); successCallBack((T) string, callBack); } else { failedCallBack("服务器错误", callBack); } } }); return call; } catch (Exception e) { Log.e(TAG, e.toString()); } return null; }
okHttp post异步请求
/** * okHttp post异步请求 * @param actionUrl 接口地址 * @param paramsMap 请求参数 * @param callBack 请求返回数据回调 * @param <T> 数据泛型 * @return */ private <T> Call requestPostByAsyn(String actionUrl, HashMap<String, String> paramsMap, final ReqCallBack<T> callBack) { try { StringBuilder tempParams = new StringBuilder(); int pos = 0; for (String key : paramsMap.keySet()) { if (pos > 0) { tempParams.append("&"); } tempParams.append(String.format("%s=%s", key, URLEncoder.encode(paramsMap.get(key), "utf-8"))); pos++; } String params = tempParams.toString(); RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, params); String requestUrl = String.format("%s/%s", BASE_URL, actionUrl); final Request request = addHeaders().url(requestUrl).post(body).build(); final Call call = mOkHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { failedCallBack("访问失败", callBack); Log.e(TAG, e.toString()); } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()) { String string = response.body().string(); Log.e(TAG, "response ----->" + string); successCallBack((T) string, callBack); } else { failedCallBack("服务器错误", callBack); } } }); return call; } catch (Exception e) { Log.e(TAG, e.toString()); } return null; }
okHttp post异步请求表单提交
/** * okHttp post异步请求表单提交 * @param actionUrl 接口地址 * @param paramsMap 请求参数 * @param callBack 请求返回数据回调 * @param <T> 数据泛型 * @return */ private <T> Call requestPostByAsynWithForm(String actionUrl, HashMap<String, String> paramsMap, final ReqCallBack<T> callBack) { try { FormBody.Builder builder = new FormBody.Builder(); for (String key : paramsMap.keySet()) { builder.add(key, paramsMap.get(key)); } RequestBody formBody = builder.build(); String requestUrl = String.format("%s/%s", BASE_URL, actionUrl); final Request request = addHeaders().url(requestUrl).post(formBody).build(); final Call call = mOkHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { failedCallBack("访问失败", callBack); Log.e(TAG, e.toString()); } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()) { String string = response.body().string(); Log.e(TAG, "response ----->" + string); successCallBack((T) string, callBack); } else { failedCallBack("服务器错误", callBack); } } }); return call; } catch (Exception e) { Log.e(TAG, e.toString()); } return null; }
接口ReqCallBack.java实现
public interface ReqCallBack<T> { /** * 响应成功 */ void onReqSuccess(T result); /** * 响应失败 */ void onReqFailed(String errorMsg); /** * 描述一个Void的返回类型 * * @author cnoss */ final class Void { volatile static Void instance = new Void(); private Void() { } public static Void instance() { return instance; } } }
/** * 统一为请求添加头信息 * @return */ private Request.Builder addHeaders() { Request.Builder builder = new Request.Builder() .addHeader("Connection", "keep-alive") .addHeader("platform", "2") .addHeader("phoneModel", Build.MODEL) .addHeader("systemVersion", Build.VERSION.RELEASE) .addHeader("appVersion", "3.2.0"); return builder; }
6.)成功与失败 回调处理
成功回调处理
/** * 统一同意处理成功信息 * @param result * @param callBack * @param <T> */ private <T> void successCallBack(final T result, final ReqCallBack<T> callBack) { okHttpHandler.post(new Runnable() { @Override public void run() { if (callBack != null) { callBack.onReqSuccess(result); } } }); }
失败回调处理
/** * 统一处理失败信息 * @param errorMsg * @param callBack * @param <T> */ private <T> void failedCallBack(final String errorMsg, final ReqCallBack<T> callBack) { okHttpHandler.post(new Runnable() { @Override public void run() { if (callBack != null) { callBack.onReqFailed(errorMsg); } } }); }
小结:基于上述基本上可以实现http之间的网络通讯,接下来我们来研究如何搞定文件的上传和下载。
Android探索之基于okHttp打造自己的网络请求(一)
标签:
原文地址:http://www.cnblogs.com/whoislcj/p/5526431.html