什么是HTTP协议
1.超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议。
2.HTTP是一个客户端和服务器端请求和应答的标准,客户端是终端用户,服务器端是网站。
3.HTTP是客户端浏览器或其他程序与Web服务器之间的应用层通信协议。
HTTP工作原理
1.客户端与服务器建立连接;
2.建立连接后,客户端香服务器端发送一个请求;
3.服务器接受到请求之后,向客户端发送响应信息;
4.客户端与服务器端断开连接;
请求报文与响应报文
1.请求报文格式:
请求行-通用信息头-请求头-实体头-报文主体
2.响应报文格式
状态行-通用信息头-响应头-实体头-报文主体
小例子:
main.xml :
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <Button
8 android:id="@+id/requestButton"
9 android:layout_width="fill_parent"
10 android:layout_height="wrap_content"
11 android:text="@string/hello" />
12
13 </LinearLayout>
14
strings.xml :
1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3
4 <string name="hello">向百度发送一次请求</string>
5 <string name="app_name">04_08_HTTP01</string>
6
7 </resources>
manifest.xml :
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="org.gaolei.http"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk android:minSdkVersion="8" />
8
9 <uses-permission android:name="android.permission.INTERNET"/>
10 <application
11 android:icon="@drawable/ic_launcher"
12 android:label="@string/app_name" >
13 <activity
14 android:name=".MainActivity"
15 android:label="@string/app_name" >
16 <intent-filter>
17 <action android:name="android.intent.action.MAIN" />
18
19 <category android:name="android.intent.category.LAUNCHER" />
20 </intent-filter>
21 </activity>
22 </application>
23
24 </manifest>
MainActivity.java :
1 package org.gaolei.http;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7
8 import org.apache.http.HttpEntity;
9 import org.apache.http.HttpResponse;
10 import org.apache.http.client.ClientProtocolException;
11 import org.apache.http.client.HttpClient;
12 import org.apache.http.client.methods.HttpGet;
13 import org.apache.http.impl.client.DefaultHttpClient;
14
15 import android.app.Activity;
16 import android.os.Bundle;
17 import android.view.View;
18 import android.view.View.OnClickListener;
19 import android.widget.Button;
20
21 public class MainActivity extends Activity {
22 private Button button;
23 private HttpResponse httpResponse;
24 private HttpEntity httpEntity;
25
26 @Override
27 public void onCreate(Bundle savedInstanceState) {
28 super.onCreate(savedInstanceState);
29 setContentView(R.layout.main);
30
31 button = (Button) findViewById(R.id.requestButton);
32 button.setOnClickListener(new OnClickListener() {
33
34 @Override
35 public void onClick(View arg0) {
36 // 生成一个请求对象
37 HttpGet httpGet = new HttpGet("http://www.baidu.com");
38
39 // 生成一个Http客户端对象
40 HttpClient httpClient = new DefaultHttpClient();
41 // 使用Http客户端发送请求对象
42 InputStream inputStream = null;
43 try {
44 httpResponse = httpClient.execute(httpGet);
45 httpEntity = httpResponse.getEntity();
46 inputStream = httpEntity.getContent();
47 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
48 String result = "";
49 String line = "";
50 while((line = reader.readLine()) != null) {
51 result = result + line;
52 }
53 } catch (ClientProtocolException e) {
54 e.printStackTrace();
55 } catch (IOException e) {
56 e.printStackTrace();
57 }finally {
58 try {
59 inputStream.close();
60 } catch (IOException e) {
61 e.printStackTrace();
62 }
63 }
64
65 }
66
67 });
68 }
69 }
70
1.HTTP请求的方法
2.使用GET方法发送请求
3.使用POST方法发送请求
main.xml:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <EditText
8 android:id="@+id/nameView"
9 android:layout_width="fill_parent"
10 android:layout_height="wrap_content" />
11
12 <EditText
13 android:id="@+id/ageView"
14 android:layout_width="fill_parent"
15 android:layout_height="wrap_content" />
16
17 <Button
18 android:id="@+id/getButton"
19 android:layout_width="fill_parent"
20 android:layout_height="wrap_content"
21 android:text="@string/getButton" />
22
23 <Button
24 android:id="@+id/postButton"
25 android:layout_width="fill_parent"
26 android:layout_height="wrap_content"
27 android:text="@string/postButton" />
28
29 </LinearLayout>
strings.xml:
1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3
4 <string name="hello">Hello World, MainActivity!</string>
5 <string name="app_name">04_09_HTTP02</string>
6 <string name="getButton">使用GET方式发送请求</string>
7 <string name="postButton">使用POST方式发送请求</string>
8
9 </resources>
manifest.xml:
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="org.gaolei.http"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk android:minSdkVersion="8" />
8
9 <uses-permission android:name="android.permission.INTERNET" />
10
11 <application
12 android:icon="@drawable/ic_launcher"
13 android:label="@string/app_name" >
14 <activity
15 android:name=".MainActivity"
16 android:label="@string/app_name" >
17 <intent-filter>
18 <action android:name="android.intent.action.MAIN" />
19
20 <category android:name="android.intent.category.LAUNCHER" />
21 </intent-filter>
22 </activity>
23 </application>
24
25 </manifest>
MainActivity.java :
1 package org.gaolei.http;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.io.UnsupportedEncodingException;
8 import java.util.ArrayList;
9 import java.util.List;
10
11 import org.apache.http.HttpEntity;
12 import org.apache.http.HttpResponse;
13 import org.apache.http.NameValuePair;
14 import org.apache.http.client.ClientProtocolException;
15 import org.apache.http.client.HttpClient;
16 import org.apache.http.client.entity.UrlEncodedFormEntity;
17 import org.apache.http.client.methods.HttpGet;
18 import org.apache.http.client.methods.HttpPost;
19 import org.apache.http.impl.client.DefaultHttpClient;
20 import org.apache.http.message.BasicNameValuePair;
21
22 import android.app.Activity;
23 import android.os.Bundle;
24 import android.view.View;
25 import android.view.View.OnClickListener;
26 import android.widget.Button;
27 import android.widget.EditText;
28
29 public class MainActivity extends Activity {
30 private Button getButton;
31 private Button postButton;
32
33 private EditText nameView;
34 private EditText ageView;
35
36 private HttpResponse httpResponse;
37 private HttpEntity httpEntity;
38
39 private String baseUrl = "http://192.168.1.100:8081/serverside/name";
40
41 @Override
42 public void onCreate(Bundle savedInstanceState) {
43 super.onCreate(savedInstanceState);
44 setContentView(R.layout.main);
45
46 getButton = (Button) findViewById(R.id.getButton);
47 getButton.setOnClickListener(new OnGetListener());
48
49 postButton = (Button) findViewById(R.id.postButton);
50 postButton.setOnClickListener(new OnPostListener());
51
52 nameView = (EditText) findViewById(R.id.nameView);
53 ageView = (EditText) findViewById(R.id.ageView);
54 }
55
56 private class OnGetListener implements OnClickListener {
57
58 @Override
59 public void onClick(View v) {
60 String name = nameView.getText().toString();
61 String age = ageView.getText().toString();
62 String url = baseUrl + "?name=" + name + "&age=" + age;
63 // 生成一个请求对象
64 HttpGet httpGet = new HttpGet(url);
65 // 生成一个Http客户端对象
66 HttpClient httpClient = new DefaultHttpClient();
67 // 使用Http客户端发送请求对象
68 InputStream inputStream = null;
69 try {
70 httpResponse = httpClient.execute(httpGet);
71 httpEntity = httpResponse.getEntity();
72 inputStream = httpEntity.getContent();
73 BufferedReader reader = new BufferedReader(
74 new InputStreamReader(inputStream));
75 String result = "";
76 String line = "";
77 while ((line = reader.readLine()) != null) {
78 result = result + line;
79 }
80 } catch (ClientProtocolException e) {
81 e.printStackTrace();
82 } catch (IOException e) {
83 e.printStackTrace();
84 } finally {
85 try {
86 inputStream.close();
87 } catch (IOException e) {
88 e.printStackTrace();
89 }
90 }
91
92 }
93
94 }
95
96 private class OnPostListener implements OnClickListener {
97
98 @Override
99 public void onClick(View v) {
100 String name = nameView.getText().toString();
101 String age = ageView.getText().toString();
102
103 NameValuePair nameValuePair1 = new BasicNameValuePair("name", name);
104 NameValuePair nameValuePair2 = new BasicNameValuePair("age", age);
105
106 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
107 nameValuePairs.add(nameValuePair1);
108 nameValuePairs.add(nameValuePair2);
109
110 HttpEntity httpEntity = null;
111 try {
112 httpEntity = new UrlEncodedFormEntity(nameValuePairs);
113 HttpPost httpPost = new HttpPost(baseUrl);
114 httpPost.setEntity(httpEntity);
115 HttpClient httpClient = new DefaultHttpClient();
116 // 使用Http客户端发送请求对象
117 InputStream inputStream = null;
118 try {
119 httpResponse = httpClient.execute(httpPost);
120 httpEntity = httpResponse.getEntity();
121 inputStream = httpEntity.getContent();
122 BufferedReader reader = new BufferedReader(
123 new InputStreamReader(inputStream));
124 String result = "";
125 String line = "";
126 while ((line = reader.readLine()) != null) {
127 result = result + line;
128 }
129 } catch (ClientProtocolException e) {
130 e.printStackTrace();
131 } catch (IOException e) {
132 e.printStackTrace();
133 } finally {
134 try {
135 inputStream.close();
136 } catch (IOException e) {
137 e.printStackTrace();
138 }
139 }
140 } catch (UnsupportedEncodingException e) {
141 e.printStackTrace();
142 }
143 }
144
145 }
146 }