Android发送GET和POST请求DEMO
来源:互联网 发布:linux 三个时间 编辑:程序博客网 时间:2024/05/21 09:11
(转自)http://blog.csdn.net/wingbin/article/details/8642887
4.0后网络访问必须单独起一个子线程访问,否则无法运行,这里有一个发送请求的工具类GetPostUtil
- public class GetPostUtil
- {
- /**
- * 向指定URL发送GET方法的请求
- *
- * @param url
- * 发送请求的URL
- * @param params
- * 请求参数,请求参数应该是name1=value1&name2=value2的形式。
- * @return URL所代表远程资源的响应
- */
- public static String sendGet(String url, String params)
- {
- String result = "";
- BufferedReader in = null;
- try
- {
- String urlName = url + "?" + params;
- URL realUrl = new URL(urlName);
- // 打开和URL之间的连接
- URLConnection conn = realUrl.openConnection();
- // 设置通用的请求属性
- conn.setRequestProperty("accept", "*/*");
- conn.setRequestProperty("connection", "Keep-Alive");
- conn.setRequestProperty("user-agent",
- "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
- // 建立实际的连接
- conn.connect();
- // 获取所有响应头字段
- Map<String, List<String>> map = conn.getHeaderFields();
- // 遍历所有的响应头字段
- for (String key : map.keySet())
- {
- System.out.println(key + "--->" + map.get(key));
- }
- // 定义BufferedReader输入流来读取URL的响应
- in = new BufferedReader(
- new InputStreamReader(conn.getInputStream()));
- String line;
- while ((line = in.readLine()) != null)
- {
- result += "\n" + line;
- }
- }
- catch (Exception e)
- {
- System.out.println("发送GET请求出现异常!" + e);
- e.printStackTrace();
- }
- // 使用finally块来关闭输入流
- finally
- {
- try
- {
- if (in != null)
- {
- in.close();
- }
- }
- catch (IOException ex)
- {
- ex.printStackTrace();
- }
- }
- return result;
- }
- /**
- * 向指定URL发送POST方法的请求
- *
- * @param url
- * 发送请求的URL
- * @param params
- * 请求参数,请求参数应该是name1=value1&name2=value2的形式。
- * @return URL所代表远程资源的响应
- */
- public static String sendPost(String url, String params)
- {
- PrintWriter out = null;
- BufferedReader in = null;
- String result = "";
- try
- {
- URL realUrl = new URL(url);
- // 打开和URL之间的连接
- URLConnection conn = realUrl.openConnection();
- // 设置通用的请求属性
- conn.setRequestProperty("accept", "*/*");
- conn.setRequestProperty("connection", "Keep-Alive");
- conn.setRequestProperty("user-agent",
- "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
- // 发送POST请求必须设置如下两行
- conn.setDoOutput(true);
- conn.setDoInput(true);
- // 获取URLConnection对象对应的输出流
- out = new PrintWriter(conn.getOutputStream());
- // 发送请求参数
- out.print(params);
- // flush输出流的缓冲
- out.flush();
- // 定义BufferedReader输入流来读取URL的响应
- in = new BufferedReader(
- new InputStreamReader(conn.getInputStream()));
- String line;
- while ((line = in.readLine()) != null)
- {
- result += "\n" + line;
- }
- }
- catch (Exception e)
- {
- System.out.println("发送POST请求出现异常!" + e);
- e.printStackTrace();
- }
- // 使用finally块来关闭输出流、输入流
- finally
- {
- try
- {
- if (out != null)
- {
- out.close();
- }
- if (in != null)
- {
- in.close();
- }
- }
- catch (IOException ex)
- {
- ex.printStackTrace();
- }
- }
- return result;
- }
- }
- Android发送GET和POST请求DEMO
- Android发送GET和POST请求DEMO
- Android关于GET和POST发送请求
- android中发送get和post请求
- Android客户端发送Get和Post请求
- get请求和post请求demo
- JS发送Get请求和Post请求
- android中post和get两种方式发送请求
- Android入门:发送HTTP的GET和POST请求
- Android入门:发送HTTP的GET和POST请求
- Android入门:发送HTTP的GET和POST请求
- Android中post和get两种方式发送请求
- Android入门:发送HTTP的GET和POST请求
- Android入门:发送HTTP的GET和POST请求
- Android Get方法和POST方法发送请求
- Android笔记---使用HttpClient发送POST和GET请求
- android studio使用volley发送post和get请求
- Android使用HttpURLConnection向服务器发送post和get请求
- Master OpenCV with Pratical Computer Vision Projects——如何编译Cartoonifier_Android
- iOS开发之网络连接判断(实例)
- ubuntu10.10编译qtopia-2.2.0 问题总结及分析
- 2013-08-21C语言编程实践课第三天
- 【SSH学习笔记】整合spring3-hibernate的小项目(一)
- Android发送GET和POST请求DEMO
- C/C++中extern关键字详解
- 使用Mac OS X系统必须了解的10条命令
- 11gR2中的SCAN监听器和本地监听器配置
- KMP算法
- hdu 1141 数学
- nhibernate连接池配置
- vim使用进阶
- IOS开发证书申请流程和给他人电脑授权