---
title: "Android的HTTP协议"
author: "lovecontry"
date: "2011-11-21T00:38:00Z"
tags: []
slug: "android-http"
draft: false
lastmod: "2011-12-07T08:57:00Z"
id: 1294599
url: "https://lovecontry.iteye.com/blog/1294599"
---

1.超文本传输协议(HTTP，HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议。所有的WWW文件都必须遵守这个标准。设计HTTP最初的目的是为了提供一种发布和接收HTML页面的方法。　　2. java接口 --------java.net.*  
3\. apache 接口---------org.apache.http.*

Apache提供的HttpCient,实现起来简单方便:

A: GET方式操作

01. public void get() {
02. String url = httpUrl + "?text1=" + text1.getText().toString()
03. \+ "&text2=" + text2.getText().toString();
04. // 创建HttpGet对象
05. HttpGet request = new HttpGet(url);
06. // 创建HttpClient对象
07. HttpClient client = new DefaultHttpClient();
08. HttpResponse httpResponse = null;
09. try {
10. httpResponse = client.execute(request);
11. if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC\_OK) {
12. text3.setText(EntityUtils.toString(httpResponse.getEntity(),
13. "utf-8"));
14. }
15. } catch (ClientProtocolException e) {
16. e.printStackTrace();
17. } catch (IOException e) {
18. e.printStackTrace();
19. }
20. }
21. public void get() {
22. String url = httpUrl + "?text1=" + text1.getText().toString()
23. \+ "&text2=" + text2.getText().toString();
24. // 创建HttpGet对象
25. HttpGet request = new HttpGet(url);
26. // 创建HttpClient对象
27. HttpClient client = new DefaultHttpClient();
28. HttpResponse httpResponse = null;
29. try {
30. httpResponse = client.execute(request);
31. if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC\_OK) {
32. text3.setText(EntityUtils.toString(httpResponse.getEntity(),
33. "utf-8"));
34. }
35. } catch (ClientProtocolException e) {
36. e.printStackTrace();
37. } catch (IOException e) {
38. e.printStackTrace();
39. }
40. }

B:POST方式操作

01. public void post() {
02. // 创建HttpPost对象
03. HttpPost httpRequest = new HttpPost(httpUrl);
04. // 创建传递参数集合
05. List params = new ArrayList();
06. params.add(new BasicNameValuePair("text1", text1.getText().toString()));
07. params.add(new BasicNameValuePair("text2", text2.getText().toString()));
08. // 设置字符集
09. try {
10. HttpEntity entity = new UrlEncodedFormEntity(params, "utf-8");
11. httpRequest.setEntity(entity);
12. // 创建连接对象
13. HttpClient client = new DefaultHttpClient();
14. // 执行连接
15. HttpResponse response = client.execute(httpRequest);
16. if (response.getStatusLine().getStatusCode() == HttpStatus.SC\_OK) {
17. text3.setText(EntityUtils.toString(response.getEntity(),
18. "utf-8"));
19. }
20. } catch (UnsupportedEncodingException e) {
21. // TODO Auto-generated catch block
22. e.printStackTrace();
23. } catch (ClientProtocolException e) {
24. // TODO Auto-generated catch block
25. e.printStackTrace();
26. } catch (IOException e) {
27. // TODO Auto-generated catch block
28. e.printStackTrace();
29. }
30. }
31. public void post() {
32. // 创建HttpPost对象
33. HttpPost httpRequest = new HttpPost(httpUrl);
34. // 创建传递参数集合
35. List params = new ArrayList();
36. params.add(new BasicNameValuePair("text1", text1.getText().toString()));
37. params.add(new BasicNameValuePair("text2", text2.getText().toString()));
38. // 设置字符集
39. try {
40. HttpEntity entity = new UrlEncodedFormEntity(params, "utf-8");
41. httpRequest.setEntity(entity);
42. // 创建连接对象
43. HttpClient client = new DefaultHttpClient();
44. // 执行连接
45. HttpResponse response = client.execute(httpRequest);
46. if (response.getStatusLine().getStatusCode() == HttpStatus.SC\_OK) {
47. text3.setText(EntityUtils.toString(response.getEntity(),
48. "utf-8"));
49. }
50. } catch (UnsupportedEncodingException e) {
51. // TODO Auto-generated catch block
52. e.printStackTrace();
53. } catch (ClientProtocolException e) {
54. // TODO Auto-generated catch block
55. e.printStackTrace();
56. } catch (IOException e) {
57. // TODO Auto-generated catch block
58. e.printStackTrace();
59. }
60. }