1.package com.yarin.android.Examples_08_02;

2.
3.import java.io.IOException;
4.import org.apache.http.HttpResponse;
5.import org.apache.http.HttpStatus;
6.import org.apache.http.client.ClientProtocolException;
7.import org.apache.http.client.HttpClient;
8.import org.apache.http.client.methods.HttpGet;
9.import org.apache.http.impl.client.DefaultHttpClient;
10.import org.apache.http.util.EntityUtils;
11.import android.app.Activity;
12.import android.content.Intent;
13.import android.os.Bundle;
14.import android.view.View;
15.import android.widget.Button;
16.import android.widget.TextView;
17.
18.public class Activity02 extends Activity {
19. /** Called when the activity is first created. */
20. @Override
21. public void onCreate(Bundle savedInstanceState) {
22. super.onCreate(savedInstanceState);
23. setContentView(R.layout.http);
24. TextView mTextView = (TextView) this.findViewById(R.id.TextView_HTTP);
25. // http位址
26. String httpUrl = "http://192.168.1.110:8080/httpget.jsp?par=HttpClient_android_Get";
27. // HttpGet連線物件
28. HttpGet httpRequest = new HttpGet(httpUrl);
29. try {
30. // 取得HttpClient物件
31. HttpClient httpclient = new DefaultHttpClient();
32. // 請求HttpClient,取得HttpResponse
33. HttpResponse httpResponse = httpclient.execute(httpRequest);
34. // 請求成功
35. if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
36. // 取得返回的字串
37. String strResult = EntityUtils.toString(httpResponse
38. .getEntity());
39. mTextView.setText(strResult);
40. } else {
41. mTextView.setText("請求錯誤!");
42. }
43. } catch (ClientProtocolException e) {
44. mTextView.setText(e.getMessage().toString());
45. } catch (IOException e) {
46. mTextView.setText(e.getMessage().toString());
47. } catch (Exception e) {
48. mTextView.setText(e.getMessage().toString());
49. }
50.
51. // 設置按鍵事件監聽
52. Button button_Back = (Button) findViewById(R.id.Button_Back);
53. /* 監聽button的事件資訊 */
54. button_Back.setOnClickListener(new Button.OnClickListener() {
55. public void onClick(View v) {
56. /* 新建一個Intent物件 */
57. Intent intent = new Intent();
58. /* 指定intent要啟動的類 */
59. intent.setClass(Activity02.this, Activity01.class);
60. /* 啟動一個新的Activity */
61. startActivity(intent);
62. /* 關閉當前的Activity */
63. Activity02.this.finish();
64. }
65. });
66. }
67.}





2. POST



1.package com.yarin.android.Examples_08_02;
2.
3.import java.io.IOException;
4.import java.util.ArrayList;
5.import java.util.List;
6.import org.apache.http.HttpEntity;
7.import org.apache.http.HttpResponse;
8.import org.apache.http.HttpStatus;
9.import org.apache.http.NameValuePair;
10.import org.apache.http.client.ClientProtocolException;
11.import org.apache.http.client.HttpClient;
12.import org.apache.http.client.entity.UrlEncodedFormEntity;
13.import org.apache.http.client.methods.HttpPost;
14.import org.apache.http.impl.client.DefaultHttpClient;
15.import org.apache.http.message.BasicNameValuePair;
16.import org.apache.http.util.EntityUtils;
17.import android.app.Activity;
18.import android.content.Intent;
19.import android.os.Bundle;
20.import android.view.View;
21.import android.widget.Button;
22.import android.widget.TextView;
23.
24.public class Activity03 extends Activity {
25. /** Called when the activity is first created. */
26. @Override
27. public void onCreate(Bundle savedInstanceState) {
28. super.onCreate(savedInstanceState);
29. setContentView(R.layout.http);
30. TextView mTextView = (TextView) this.findViewById(R.id.TextView_HTTP);
31. // http位址
32. String httpUrl = "http://192.168.1.110:8080/httpget.jsp";
33. // HttpPost連線物件
34. HttpPost httpRequest = new HttpPost(httpUrl);
35. // 使用NameValuePair來保存要傳遞的Post參數
36. List<NameValuePair> params = new ArrayList<NameValuePair>();
37. // 添加要傳遞的參數
38. params.add(new BasicNameValuePair("par", "HttpClient_android_Post"));
39. try {
40. // 設置字元集
41. HttpEntity httpentity = new UrlEncodedFormEntity(params, "gb2312");
42. // 請求httpRequest
43. httpRequest.setEntity(httpentity);
44. // 取得預設的HttpClient
45. HttpClient httpclient = new DefaultHttpClient();
46. // 取得HttpResponse
47. HttpResponse httpResponse = httpclient.execute(httpRequest);
48. // HttpStatus.SC_OK表示連接成功
49. if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
50. // 取得返回的字串
51. String strResult = EntityUtils.toString(httpResponse.getEntity());
52. mTextView.setText(strResult);
53. } else {
54. mTextView.setText("請求錯誤!");
55. }
56. } catch (ClientProtocolException e) {
57. mTextView.setText(e.getMessage().toString());
58. } catch (IOException e) {
59. mTextView.setText(e.getMessage().toString());
60. } catch (Exception e) {
61. mTextView.setText(e.getMessage().toString());
62. }
63. // 設置按鍵事件監聽
64. Button button_Back = (Button) findViewById(R.id.Button_Back);
65. /* 監聽button的事件資訊 */
66. button_Back.setOnClickListener(new Button.OnClickListener() {
67. public void onClick(View v) {
68. /* 新建一個Intent物件 */
69. Intent intent = new Intent();
70. /* 指定intent要啟動的類 */
71. intent.setClass(Activity03.this, Activity01.class);
72. /* 啟動一個新的Activity */
73. startActivity(intent);
74. /* 關閉當前的Activity */
75. Activity03.this.finish();
76. }
77. });
78. }
79.}
1.評論:
2.ApacheGet : HttpGet httpRequest = new HttpGet(httpUrl);
3.ApachePost: HttpPost httpRequest = new HttpPost(httpUrl);
4.
5.
6.// 取得HttpClient物件
7.HttpClient httpclient = new DefaultHttpClient();
8.// 請求HttpClient,取得HttpResponse
9.HttpResponse httpResponse = httpclient.execute(httpRequest);
10.
11.
12.
13.// HttpStatus.SC_OK表示連接成功
14.if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
15.// 取得返回的字串
16.String strResult = EntityUtils.toString(httpResponse.getEntity());
17.mTextView.setText(strResult);
18.} else {
19.mTextView.setText("請求錯誤!");
20. }
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()