Android Http请求方法汇总
来源:互联网 发布:arm linux android 编辑:程序博客网 时间:2024/06/02 09:22
Android Http请求方法汇总
原帖:http://www.open-open.com/lib/view/open1351324240738.html
1. 使用JDK中HttpURLConnection访问网络资源
(1)get请求
public String executeHttpGet() {String result = null;URL url = null;HttpURLConnection connection = null;InputStreamReader in = null;try {url = new URL("http://10.0.2.2:8888/data/get/?token=alexzhou");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;}
注意:因为是通过android模拟器访问本地pc服务端,所以不能使用localhost和127.0.0.1,使用127.0.0.1会访问模拟器自身。Android系统为实现通信将PC的IP设置为10.0.2.2
(2)post请求
public String executeHttpPost() {String result = null;URL url = null;HttpURLConnection connection = null;InputStreamReader in = null;try {url = new URL("http://10.0.2.2:8888/data/post/");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("token=alexzhou");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;}
如果参数中有中文的话,可以使用下面的方式进行编码解码:
URLEncoder.encode("测试","utf-8")URLDecoder.decode("测试","utf-8");
2.使用Apache的HttpClient访问网络资源
(1)get请求
public String executeGet() {String result = null;BufferedReader reader = null;try {HttpClient client = new DefaultHttpClient();HttpGet request = new HttpGet();request.setURI(new URI("http://10.0.2.2:8888/data/get/?token=alexzhou"));HttpResponse response = client.execute(request);reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));StringBuffer strBuffer = new StringBuffer("");String line = null;while ((line = reader.readLine()) != null) {strBuffer.append(line);}result = strBuffer.toString();} catch (Exception e) {e.printStackTrace();} finally {if (reader != null) {try {reader.close();reader = null;} catch (IOException e) {e.printStackTrace();}}}return result;}
(2)post请求
public String executePost() {String result = null;BufferedReader reader = null;try {HttpClient client = new DefaultHttpClient();HttpPost request = new HttpPost();request.setURI(new URI("http://10.0.2.2:8888/data/post/"));List<NameValuePair> postParameters = new ArrayList<NameValuePair>();postParameters.add(new BasicNameValuePair("token", "alexzhou"));UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);request.setEntity(formEntity);HttpResponse response = client.execute(request);reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));StringBuffer strBuffer = new StringBuffer("");String line = null;while ((line = reader.readLine()) != null) {strBuffer.append(line);}result = strBuffer.toString();} catch (Exception e) {e.printStackTrace();} finally {if (reader != null) {try {reader.close();reader = null;} catch (IOException e) {e.printStackTrace();}}}return result;}
- Android Http请求方法汇总
- Android Http请求方法汇总
- Android Http请求方法汇总
- Android Http请求方法汇总
- Android Http请求方法汇总
- Android Http请求方法汇总
- Android Http请求方法汇总
- Android Http请求方法汇总
- Android Http请求方法汇总
- Android Http请求方法汇总
- Android Http请求方法汇总
- Android Http请求方法汇总
- Android Http请求方法汇总
- Android Http请求方法汇总
- Android Http请求方法汇总
- Android Http请求方法汇总
- Android Http请求方法汇总
- Android Http请求方法汇总
- Android training(1)-Building a Dynamic UI with Fragments(用碎片创造一个动态的UI)(A)
- 编程之美 1的数目
- android 源码下载、同步、编译、提交
- 卡特兰数的应用
- unix路由表flag标志位
- Android Http请求方法汇总
- 二十一个巧记英语语法的口诀
- HDU 4678 Mine 解题报告
- 子网掩码换算和子网的划分详解
- SharePoint 2013 显示用户Lync是否在线(集成Lync)
- 在SourceInsight 中配置单行,多行注释快捷键
- 编程之美 寻找最大的K个数
- OpenSSL之PKey的EVP封装
- 最长递增子序列的求解--动态规划求解