简单封装Http的Get和Post请求
来源:互联网 发布:php 公钥加密 编辑:程序博客网 时间:2024/05/15 06:30
虽然现在有许多成型的Http框架,比如OkHttp、Volley,但是有时候我们仅仅需要一个很简单的功能,比如仅需要一个Get和Post,那么我们用这些库就有种杀机用宰牛刀的感觉。于是就想着自己去封装一个简单的类来实现这部分功能。
以下是我的工具类:
public class HttpUtil { private HttpUtil() { } /** * get方法请求数据 * * @param url 请求地址 * @param params 请求参数 * @param callBack 回调接口 */ public static void get(final String url, final Map<String, String> params, final HttpResponseCallBack callBack) { if (callBack != null) { new Thread(new Runnable() { @Override public void run() { try { //拼装请求参数列表 final StringBuilder sb = new StringBuilder(64); if (params != null) { sb.append("?"); for (Map.Entry<String, String> entry : params.entrySet()) { sb.append(entry.getKey()); sb.append("="); sb.append(entry.getValue()); sb.append("&"); } sb.deleteCharAt(sb.length() - 1); } //构建新连接 URL httpUrl = new URL(url + sb.toString()); LogUtil.i(url + sb.toString()); HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection(); conn.setConnectTimeout(3000); conn.setReadTimeout(3000); conn.setRequestMethod("GET"); BufferedReader br = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8")); //接受返回数据 sb.setLength(0); String strTemp; while ((strTemp = br.readLine()) != null) { sb.append(strTemp).append('\n'); } conn.disconnect(); AsyncRun.run(new Runnable() { @Override public void run() { callBack.onSuccess(sb.toString()); } }); } catch (final IOException e) { AsyncRun.run(new Runnable() { @Override public void run() { callBack.onFailure("失败信息:", e); } }); } } }).start(); } } /** * post方法请求数据 * * @param url 请求地址 * @param params 请求参数 * @param callBack 回调地址 */ public static void post(final String url, final Map<String, String> params, final HttpResponseCallBack callBack) { if (callBack != null) { new Thread(new Runnable() { @Override public void run() { try { final StringBuilder sb = new StringBuilder(64); URL httpUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection(); conn.setConnectTimeout(3000); conn.setReadTimeout(3000); conn.setRequestMethod("POST"); conn.setDoOutput(true); //拼装请求参数列表 if (params != null) { for (Map.Entry<String, String> entry : params.entrySet()) { sb.append(entry.getKey()); sb.append("="); sb.append(entry.getValue()); sb.append("&"); } sb.deleteCharAt(sb.length() - 1); conn.getOutputStream().write(sb.toString().getBytes()); } BufferedReader br = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8")); //接受返回数据 sb.setLength(0); String strTemp; while ((strTemp = br.readLine()) != null) { sb.append(strTemp).append('\n'); } conn.disconnect(); AsyncRun.run(new Runnable() { @Override public void run() { callBack.onSuccess(sb.toString()); } }); } catch (final IOException e) { AsyncRun.run(new Runnable() { @Override public void run() { callBack.onFailure("失败信息:", e); } }); } } }).start(); } } /** * 获取回调 */ public interface HttpResponseCallBack { void onSuccess(String result); void onFailure(String result, Exception e); }}
public final class AsyncRun { public static void run(Runnable runnable){ Handler handler = new Handler(Looper.getMainLooper()); handler.post(runnable); }}
HashMap<String, String> request = new HashMap<>(2); map.put("ie", "UTF-8"); map.put("wd", "CSDN"); HttpUtil.post("https://www.baidu.com/s", request, new HttpUtil.HttpResponseCallBack() { @Override public void onSuccess(String result) { Log.i("TAG", result); //这里已经实在主线程中运行了 //所以完全可以直接调用TextView之类的控件来显示我们的回调内容 } @Override public void onFailure(String result, Exception e) { Log.i("TAG", </span>result + e.toString()); } });
为了简单起见,我把参数用一个Map来封装,这样不仅代码看起来更加简介,使用的时候已能够很方便了!
2 0
- 简单封装Http的Get和Post请求
- 基于HttpClient的Get/Post的http请求简单封装
- [Java]自定义Jar库,Http简单的Get和Post请求封装
- 简单封装ajax的get和post请求
- Http的get和post请求简单应用
- 封装了http Get请求和Post请求
- HTTP的GET请求和POST请求
- HTTP请求的GET和POST请求
- HTTP的GET请求和POST请求
- 简单的POST请求和get请求
- angular $q封装$http get和post请求
- http请求,get和post的异同
- http请求,get和post的异同
- HTTP的GET和POST请求
- HTTP请求:POST和GET的区别
- Http的GET和POST请求
- http的get和post请求
- http请求的get和post
- 微信滑动底部菜单栏-参考方法
- Lightoj 1003
- Android静态安全检测 -> allowBackup标志位
- C++的多态性
- 文件系统(ext2) inode、block、superblock概念整理
- 简单封装Http的Get和Post请求
- matlab小波滤波器使用
- Guava base -- MoreObjects
- 高级排序算法之希尔排序
- 剑指Offer----面试题20:顺时针打印矩阵
- 文字语录
- Lightoj 1004
- Python(1)
- Linux+PHP设置北京时间