Android中的HTTP请求(GET/POST)
来源:互联网 发布:如何手机关闭淘宝店 编辑:程序博客网 时间:2024/05/16 03:09
public String executeHttpGet(String urlStr) {String result = null;URL url = null;HttpURLConnection connection = null;InputStreamReader in = null;if (urlStr != null && urlStr.length() > 0) {try {url = new URL(urlStr);connection = (HttpURLConnection) url.openConnection();in = new InputStreamReader(connection.getInputStream());BufferedReader bufferedReader = new BufferedReader(in);StringBuffer strBuffer = new StringBuffer();String line = null;while ((line = bufferedReader.readLine()) != null) {strBuffer.append(line);}result = strBuffer.toString();} catch (Exception e) {e.printStackTrace();} finally {if (connection != null) {connection.disconnect();}if (in != null) {try {in.close();} catch (IOException e) {e.printStackTrace();}}}}return result;}public String executeHttpPost(String urlStr, String postStr) {String result = null;URL url = null;HttpURLConnection connection = null;InputStreamReader in = null;if (urlStr != null && urlStr.length() > 0) {try {url = new URL(urlStr);connection = (HttpURLConnection) url.openConnection();connection.setDoInput(true);connection.setDoOutput(true);connection.setRequestMethod("POST");connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");connection.setRequestProperty("Charset", "utf-8");DataOutputStream dop = new DataOutputStream(connection.getOutputStream());dop.writeBytes(postStr);dop.flush();dop.close();in = new InputStreamReader(connection.getInputStream());BufferedReader bufferedReader = new BufferedReader(in);StringBuffer strBuffer = new StringBuffer();String line = null;while ((line = bufferedReader.readLine()) != null) {strBuffer.append(line);}result = strBuffer.toString();} catch (Exception e) {e.printStackTrace();} finally {if (connection != null) {connection.disconnect();}if (in != null) {try {in.close();} catch (IOException e) {e.printStackTrace();}}}}return result;}
0 0
- Android中的HTTP请求(GET/POST)
- Http中的请求-get-post
- Android Http get post请求
- Android Http get post请求
- Android Http get post请求
- Android Http get post请求
- Android Http get post请求
- Android Http get post请求
- Android Http get post请求
- Android Http get post请求
- HTTP中的GET请求与POST请求
- HTTP中的get请求与post请求
- Android中的http带参数网络请求,GET和POST
- HTTP协议中的GET、POST请求
- Android 发送请求 HTTP GET/POST
- [转]Android Http get post请求
- 浅析 - Android http GET/POST 请求
- Android中HTTP Post和Get请求
- C# 输出格式化
- SVN常用命令
- ORA-04063: package body "SYS.DBMS_RCVMAN" has errors
- asmack实现端对端的消息回执,避免网络不稳定引起消息丢失
- 2014年美国数学建模竞赛C题总结
- Android中的HTTP请求(GET/POST)
- 收徒弟了~~资深Java开发经验~~包教包会
- Java 中重载与复写的区别、super与this的比较
- Linux下ssh断开时能继续执行任务
- 网盘系列:企业网盘开发基础知识
- 师父教的oracle:
- 从零开始学新浪微博分享和第三方登陆(附源码下载) .
- Jmeter和LR上传文件和下载
- 段家乐在东莞与500妹子打战100天!