封装OkHttp
来源:互联网 发布:mac os 下载速度慢 编辑:程序博客网 时间:2024/06/14 03:03
一.创建接口
public interface CallBack { void Onsuccess(String tag, Object o); void Onfailed(String tag, Exception e);}
二.创建gson工具包
public class GsonUtils { private static Gson instance; private GsonUtils() { } public static Gson getInstance() { if (instance == null) { instance = new Gson(); } return instance; }}
三.封装OkHttp
public class HttpUtils { private static final String TAG = "HttpUtils"; private static volatile HttpUtils instance; Handler handler = new Handler(); private HttpUtils() { } public static HttpUtils getInstance() { if (null == instance) { synchronized (HttpUtils.class) { if (instance == null) { instance = new HttpUtils(); } } } return instance; } public void get(String url, Map<String, String> map, final CallBack callBack, final Class cls, final String tag) { if (TextUtils.isEmpty(url)) { return; } StringBuffer sb = new StringBuffer(); sb.append(url); // 如果包含?说明是2.3类型 if (url.contains("?")) { // 如果包含?并且?是最后一位,对应是2类型 if (url.indexOf("?") == url.length() - 1) { } else { // 如果包含?并且?不是最后一位,对应是3类型 sb.append("&"); } } else { // 不包含?,对应的1类型 sb.append("?"); } // 遍历map集合进行拼接,拼接的形式是 key=value& for (Map.Entry<String, String> entry : map.entrySet()) { sb.append(entry.getKey()) .append("=") .append(entry.getValue()) .append("&"); } // 如果存在&号,把最后一个&去掉 if (sb.indexOf("&") != -1) { sb.deleteCharAt(sb.lastIndexOf("&")); } Log.e("-=-=-=-=-=-=-=url路径", sb.toString()); // OkHttpClient client = new OkHttpClient(); Request build = new Request.Builder() .get() .url(sb.toString()) .build(); Call call = client.newCall(build); call.enqueue(new Callback() { @Override public void onFailure(Call call, final IOException e) { handler.post(new Runnable() { @Override public void run() { Log.e("-=-=-=-=-", e.getMessage()); //通过自己传过来的回调接口对象回传回去 callBack.Onfailed(tag, e); } }); } @Override public void onResponse(final Call call, Response response) throws IOException { final String string = response.body().string(); Log.e("---------------", string); //请求成功之后做解析,通过自己回调的接口传值 handler.post(new Runnable() { @Override public void run() { Object o; if (TextUtils.isEmpty(string)) { o = null; } else { o = GsonUtils.getInstance().fromJson(string, cls); } Log.e("==========", o.toString()); callBack.Onsuccess(tag, o); } }); } }); } /** * 封装的post请求 * * @param url * @param map * @param callBack * @param cls */ public void post(String url, Map<String, String> map, final CallBack callBack, final Class cls, final String tag) { OkHttpClient client = new OkHttpClient(); FormBody.Builder builder = new FormBody.Builder(); for (Map.Entry<String, String> entry : map.entrySet()) { builder.add(entry.getKey(), entry.getValue()); } FormBody body = builder.build(); final Request request = new Request.Builder() .url(url) .post(body) .build(); Call call = client.newCall(request); call.enqueue(new okhttp3.Callback() { @Override public void onFailure(final Call call, final IOException e) { Log.e(TAG, "onFailure: " + e.getCause().getStackTrace() + e.getMessage()); handler.post(new Runnable() { @Override public void run() { //通过自己传过来的回调接口对象回传回去 callBack.Onfailed(tag, e); } }); } @Override public void onResponse(Call call, Response response) throws IOException { final String result = response.body().string(); Log.i(TAG, "onResponse: " + result); final Object o = GsonUtils.getInstance().fromJson(result, cls); handler.post(new Runnable() { @Override public void run() { Object o; if (TextUtils.isEmpty(result)) { o = null; } else { o = GsonUtils.getInstance().fromJson(result, cls); } Log.e("==========", o.toString()); callBack.Onsuccess(tag, o); } }); } }); }}
阅读全文
0 0
- okhttp封装okhttp-utils
- 封装OKHttp
- 封装OkHttp
- okhttp封装
- 封装OKHttp
- 封装OKHttp
- Okhttp封装
- OkHttp 封装
- OkHttp封装
- OKHttp封装
- Okhttp封装
- okHttp封装
- OkHttp封装
- Okhttp封装
- 封装okhttp
- okhttp封装
- 封装okhttp
- Okhttp封装
- Linux centOS7 开机设置进入命令行
- error -5008: intel64 or amd64 must be specified in the template of the summary
- 微课堂v2 2.1.3最新官方微赞微擎原版解密开源版破解在线教育通用功能模块掌上教育专家
- 替换字符串(以替换空格为例)
- c3p0工具jdbcutils工具类模板
- 封装OkHttp
- (Slide)Attention Mechanism注意力机制
- zynq的pl中断在linux下的配置及中断驱动
- BASE64 编码规则
- 分页查询产品信息案例;第一步:创建产品页面,向服务器发送请求(获取所有产品信息)
- ubuntu查看opencv版本
- JavaScript学习笔记_ReactRouter使用教程
- iOS 代码规范~分类扩充方法、浮点型、抽取方法、耗时操作
- 分页查询产品信息,第二步:创建一个servlet来接收请求,获取所有的产品信息