`
dyg001
  • 浏览: 27220 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论
阅读更多
1.超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议。所有的WWW文件都必须遵守这个标准。设计HTTP最初的目的是为了提供一种发布和接收HTML页面的方法。  2. java接口 --------java.net.*
3. apache 接口---------org.apache.http.*


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

A: GET方式操作

1.public void get() {

2.  String url = httpUrl + "?text1=" + text1.getText().toString()

3.  + "&text2=" + text2.getText().toString();

4.  // 创建HttpGet对象

5.  HttpGet request = new HttpGet(url);

6.  // 创建HttpClient对象

7.  HttpClient client = new DefaultHttpClient();

8.  HttpResponse httpResponse = null;

9.  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方式操作

1.public void post() {

2.

3.  // 创建HttpPost对象

4.

5.  HttpPost httpRequest = new HttpPost(httpUrl);

6.

7.  // 创建传递参数集合

8.

9.  List params = new ArrayList();

10.

11.  params.add(new BasicNameValuePair("text1", text1.getText().toString()));

12.

13.  params.add(new BasicNameValuePair("text2", text2.getText().toString()));

14.

15.  // 设置字符集

16.

17.  try {

18.

19.  HttpEntity entity = new UrlEncodedFormEntity(params, "utf-8");

20.

21.  httpRequest.setEntity(entity);

22.

23.  // 创建连接对象

24.

25.  HttpClient client = new DefaultHttpClient();

26.

27.  // 执行连接

28.

29.  HttpResponse response = client.execute(httpRequest);

30.

31.  if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

32.

33.  text3.setText(EntityUtils.toString(response.getEntity(),

34.

35.  "utf-8"));

36.

37.  }

38.

39.  } catch (UnsupportedEncodingException e) {

40.

41.  // TODO Auto-generated catch block

42.

43.  e.printStackTrace();

44.

45.  } catch (ClientProtocolException e) {

46.

47.  // TODO Auto-generated catch block

48.

49.  e.printStackTrace();

50.

51.  } catch (IOException e) {

52.

53.  // TODO Auto-generated catch block

54.

55.  e.printStackTrace();

56.

57.  }

58.

59.  }

60.

61.  public void post() {

62.

63.  // 创建HttpPost对象

64.

65.  HttpPost httpRequest = new HttpPost(httpUrl);

66.

67.  // 创建传递参数集合

68.

69.  List params = new ArrayList();

70.

71.  params.add(new BasicNameValuePair("text1", text1.getText().toString()));

72.

73.  params.add(new BasicNameValuePair("text2", text2.getText().toString()));

74.

75.  // 设置字符集

76.

77.  try {

78.

79.  HttpEntity entity = new UrlEncodedFormEntity(params, "utf-8");

80.

81.  httpRequest.setEntity(entity);

82.

83.  // 创建连接对象

84.

85.  HttpClient client = new DefaultHttpClient();

86.// 执行连接

87.  HttpResponse response = client.execute(httpRequest);

88.  if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

89.  text3.setText(EntityUtils.toString(response.getEntity(),

90.  "utf-8"));

91.  }

92.  } catch (UnsupportedEncodingException e) {

93.  // TODO Auto-generated catch block

94.  e.printStackTrace();

95.  } catch (ClientProtocolException e) {

96.  // TODO Auto-generated catch block

97.  e.printStackTrace();

98.  } catch (IOException e) {

99.  // TODO Auto-generated catch block

100.  e.printStackTrace();

101.  }

102.  }




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics