Android 原生HttpURLConnection网络请求工具类(get post)
来源:互联网 发布:辐射4捏脸数据存档 编辑:程序博客网 时间:2024/05/29 07:25
public class NetworkUtil { /* * 传入一个Url地址 返回一个JSON字符串 * 网络请求的情况分析: * 如果是404 500 ... 代表网络(Http协议)请求失败 * 200 服务器返回成功 * 业务成功 /业务失败 * */ public static String doGet(String urlPath) { try { URL url = new URL(urlPath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); if (conn.getResponseCode() == 200) { InputStream is = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); return reader.readLine(); } } catch (Exception e) { e.printStackTrace(); } return "{ \"success\": false,\n \"errorMsg\": \"后台服务器开小差了!\",\n \"result\":{}}"; } /* * 传入一个Url地址 返回一个JSON字符串 * */ public static String doPost(String urlPath, HashMap<String, String> paramsMap) { try { URL url = new URL(urlPath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); //-------------------------------- conn.setDoOutput(true); conn.getOutputStream().write(getParams(paramsMap).getBytes()); //-------------------------------- if (conn.getResponseCode() == 200) { InputStream is = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); return reader.readLine(); } } catch (Exception e) { e.printStackTrace(); } return "{ \"success\": false,\n \"errorMsg\": \"后台服务器开小差了!\",\n \"result\":{}}"; } private static String getParams(HashMap<String, String> paramsMap) { String result = ""; for (HashMap.Entry<String, String> entity : paramsMap.entrySet()) { result += "&" + entity.getKey() + "=" + entity.getValue(); } return result.substring(1); }}
阅读全文
0 0
- Android 原生HttpURLConnection网络请求工具类(get post)
- Android网络请求,Get方式请求,HttpURLConnection请求工具类
- HttpURLConnection,get,post,网络请求
- HttpURLConnection 网络请求 get post
- HttpURLConnection:POST和GET 网络请求
- 网络请求get/post工具类:NetUtil
- 原生 NSURL 网络请求 GET POST
- Android 使用HttpURLConnection发送Post/Get请求
- android使用HttpURLConnection发送Post/Get请求
- Android HttpURLConnection get、post 访问网络
- android网络编程之HttpUrlConnection--POST请求
- HttpUrlConnection 网络请求工具类
- HttpURLConnection网络请求工具类
- HttpURLConnection网络请求工具类
- Android中使用HttpURLConnection和HttpClient实现GET和POST请求访问网络
- 第十二篇:Android GET请求获取网络数据以及POST数据(使用HttpURLConnection)
- Android-----HttpURLConnection的 get 请求和 post 请求
- 网络请求----HttpURLConnection的get,post和图片加载
- 如何快速转载CSDN中的博客
- Linux下如何查看tomcat是否启动-系统日志等
- android之.9图使用
- AngularJS Service、Factory、Provider 的理解与使用区别
- 多线程的几种实现方式(简易版)--Callable接口介绍
- Android 原生HttpURLConnection网络请求工具类(get post)
- 阿里面试经历(二)
- Redux
- 1030. Travel Plan (30)
- JavaSE学习总结-坦克大战
- 新生多校第6场 LLM找对象(DP+离散化)
- 包装类
- MySQL高阶之事务、存储结构、索引、触发器
- Bigtable: A Distributed Storage System for Structured Data : part10 Related Work