第一种:基于http协议通过get方式提交参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public
static
String get_save(String name, String phone) {
/**
* 出现乱码原因有2个 提交参数没有对中文编码,解决方法:使用URLEncoder.encode(xx,xx)对要提交的汉字转码
* tomatCAT服务器默认采用iso859-1编码,解决方法:把PHP页面保存为UTF-8格式
*/
String path =
"http://192.168.0.117/testxml/web.php"
;
Map<String, String> params =
new
HashMap<String, String>();
try
{
params.put(
"name"
, URLEncoder.encode(name,
"UTF-8"
));
params.put(
"phone"
, phone);
return
sendgetrequest(path, params);
}
catch
(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return
"提交失败"
;
}
get_save()
|
1.对多个参数的封装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
private
static
String sendgetrequest(String path, Map<String, String> params)
throws
Exception {
// path="http://192.168.0.117/testxml/web.php?name=xx&phone=xx";
StringBuilder url =
new
StringBuilder(path);
url.append(
"?"
);
for
(Map.Entry<String, String> entry : params.entrySet()) {
url.append(entry.getKey()).append(
"="
);
url.append(entry.getValue()).append(
"&"
);
}
url.deleteCharAt(url.length() -
1
);
URL url1 =
new
URL(url.toString());
HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
conn.setConnectTimeout(
5000
);
conn.setRequestMethod(
"GET"
);
if
(conn.getResponseCode() ==
200
) {
InputStream instream = conn.getInputStream();
ByteArrayOutputStream outstream =
new
ByteArrayOutputStream();
byte
[] buffer =
new
byte
[
1024
];
while
(instream.read(buffer) != -
1
) {
outstream.write(buffer);
}
instream.close();
return
outstream.toString().trim();
}
return
"连接失败"
;
}
sendgetrequest
|
2.提交参数
第二种:基于http协议通过post方式提交参数
1.对多个参数封装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public
static
String post_save(String name, String phone) {
/**
* 出现乱码原因有2个 提交参数没有对中文编码,解决方法:使用URLEncoder.encode(xx,xx)对要提交的汉字转码
* tomatCAT服务器默认采用iso859-1编码,解决方法:把PHP页面保存为UTF-8格式
*/
String path =
"http://192.168.0.117/testxml/web.php"
;
Map<String, String> params =
new
HashMap<String, String>();
try
{
params.put(
"name"
, URLEncoder.encode(name,
"UTF-8"
));
params.put(
"phone"
, phone);
return
sendpostrequest(path, params);
}
catch
(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return
"提交失败"
;
}
post_save
|
2.提交参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
private
static
String sendpostrequest(String path,
Map<String, String> params)
throws
Exception {
// name=xx&phone=xx
StringBuilder data =
new
StringBuilder();
if
(params !=
null
&& !params.isEmpty()) {
for
(Map.Entry<String, String> entry : params.entrySet()) {
data.append(entry.getKey()).append(
"="
);
data.append(entry.getValue()).append(
"&"
);
}
data.deleteCharAt(data.length() -
1
);
}
byte
[] entiy = data.toString().getBytes();
// 生成实体数据
URL url =
new
URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(
5000
);
conn.setRequestMethod(
"POST"
);
conn.setDoOutput(
true
);
// 允许对外输出数据
conn.setRequestProperty(
"Content-Type"
,
"application/x-www-form-urlencoded"
);
conn.setRequestProperty(
"Content-Length"
, String.valueOf(entiy.length));
OutputStream outstream = conn.getOutputStream();
outstream.write(entiy);
if
(conn.getResponseCode() ==
200
) {
InputStream instream = conn.getInputStream();
ByteArrayOutputStream byteoutstream =
new
ByteArrayOutputStream();
byte
[] buffer =
new
byte
[
1024
];
while
(instream.read(buffer) != -
1
) {
byteoutstream.write(buffer);
}
instream.close();
return
byteoutstream.toString().trim();
}
return
"提交失败"
;
}
sendpostrequest()
|
第三种:基于httpclient类通过post方式提交参数
1.对多个参数的封装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public
static
String httpclient_postsave(String name, String phone) {
/**
* 出现乱码原因有2个 提交参数没有对中文编码,解决方法:使用URLEncoder.encode(xx,xx)对要提交的汉字转码
* tomatCAT服务器默认采用iso859-1编码,解决方法:把PHP页面保存为UTF-8格式
*/
String path =
"http://192.168.0.117/testxml/web.php"
;
Map<String, String> params =
new
HashMap<String, String>();
try
{
params.put(
"name"
, name);
params.put(
"phone"
, phone);
return
sendhttpclient_postrequest(path, params);
}
catch
(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return
"提交失败"
;
}
httpclient_postsave
|
2.提交参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
private
static
String sendhttpclient_postrequest(String path,Map<String, String> params) {
List<NameValuePair> pairs =
new
ArrayList<NameValuePair>();
for
(Map.Entry<String, String> entry : params.entrySet()) {
pairs.add(
new
BasicNameValuePair(entry.getKey(), entry.getValue()));
}
try
{
UrlEncodedFormEntity entity=
new
UrlEncodedFormEntity(pairs,
"UTF-8"
);
HttpPost httpost=
new
HttpPost(path);
httpost.setEntity(entity);
HttpClient client=
new
DefaultHttpClient();
HttpResponse response= client.execute(httpost);
if
(response.getStatusLine().getStatusCode()==
200
){
return
EntityUtils.toString(response.getEntity(),
"utf-8"
);
}
}
catch
(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return
null
;
}
sendhttpclient_postrequest()
|
第四种方式:基于httpclient通过get提交参数
1.多个参数的封装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public
static
String httpclient_getsave(String name, String phone) {
/**
* 出现乱码原因有2个 提交参数没有对中文编码,解决方法:使用URLEncoder.encode(xx,xx)对要提交的汉字转码
* tomatCAT服务器默认采用iso859-1编码,解决方法:把PHP页面保存为UTF-8格式
*/
String path =
"http://192.168.0.117/testxml/web.php"
;
Map<String, String> params =
new
HashMap<String, String>();
try
{
params.put(
"name"
, name);
params.put(
"phone"
, phone);
return
sendhttpclient_getrequest(path, params);
}
catch
(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return
"提交失败"
;
}
httpclient_getsave
|
2.提交参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
private
static
String sendhttpclient_getrequest(String path,Map<String, String> map_params) {
List<BasicNameValuePair> params =
new
ArrayList<BasicNameValuePair>();
for
(Map.Entry<String, String> entry : map_params.entrySet()) {
params.add(
new
BasicNameValuePair(entry.getKey(), entry.getValue()));
}
String param=URLEncodedUtils.format(params,
"UTF-8"
);
HttpGet getmethod=
new
HttpGet(path+
"?"
+param);
HttpClient httpclient=
new
DefaultHttpClient();
try
{
HttpResponse response=httpclient.execute(getmethod);
if
(response.getStatusLine().getStatusCode()==
200
){
return
EntityUtils.toString(response.getEntity(),
"utf-8"
);
}
}
catch
(ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return
null
;
}
sendhttpclient_getrequest()
|
4.种方式完整测试案例源码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package
caicai.web;
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.EditText;
import
android.widget.TextView;
public
class
Cai_webActivity
extends
Activity {
TextView success;
String name;
String phone;
EditText name_view;
EditText phone_view;
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
name_view=(EditText) findViewById(R.id.name);
phone_view=(EditText) findViewById(R.id.phone);
success=(TextView) findViewById(R.id.success);
}
public
void
get_save(View v){
name=name_view.getText().toString();
phone=phone_view.getText().toString();
success.setText(service.get_save(name,phone));
}
public
void
post_save(View v){
name=name_view.getText().toString();
phone=phone_view.getText().toString();
success.setText(service.post_save(name,phone));
}
public
void
httpclient_postsave(View v){
name=name_view.getText().toString();
phone=phone_view.getText().toString();
success.setText(service.httpclient_postsave(name,phone));
}
public
void
httpclient_getsave(View v){
name=name_view.getText().toString();
phone=phone_view.getText().toString();
success.setText(service.httpclient_getsave(name,phone));
}
}
Cai_webActivity.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
package
caicai.web;
import
java.io.ByteArrayOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.OutputStream;
import
java.net.HttpURLConnection;
import
java.net.URL;
import
java.net.URLEncoder;
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;
import
org.apache.http.HttpResponse;
import
org.apache.http.NameValuePair;
import
org.apache.http.client.ClientProtocolException;
import
org.apache.http.client.HttpClient;
import
org.apache.http.client.entity.UrlEncodedFormEntity;
import
org.apache.http.client.methods.HttpGet;
import
org.apache.http.client.methods.HttpPost;
import
org.apache.http.client.utils.URLEncodedUtils;
import
org.apache.http.impl.client.DefaultHttpClient;
import
org.apache.http.message.BasicNameValuePair;
import
org.apache.http.util.EntityUtils;
public
class
service {
public
static
String get_save(String name, String phone) {
/**
* 出现乱码原因有2个 提交参数没有对中文编码,解决方法:使用URLEncoder.encode(xx,xx)对要提交的汉字转码
* tomatCAT服务器默认采用iso859-1编码,解决方法:把PHP页面保存为UTF-8格式
*/
String path =
"http://192.168.0.117/testxml/web.php"
;
Map<String, String> params =
new
HashMap<String, String>();
try
{
params.put(
"name"
, URLEncoder.encode(name,
"UTF-8"
));
params.put(
"phone"
, phone);
return
sendgetrequest(path, params);
}
catch
(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return
"提交失败"
;
}
private
static
String sendgetrequest(String path, Map<String, String> params)
throws
Exception {
// path="http://192.168.0.117/testxml/web.php?name=xx&phone=xx";
StringBuilder url =
new
StringBuilder(path);
url.append(
"?"
);
for
(Map.Entry<String, String> entry : params.entrySet()) {
url.append(entry.getKey()).append(
"="
);
url.append(entry.getValue()).append(
"&"
);
}
url.deleteCharAt(url.length() -
1
);
URL url1 =
new
URL(url.toString());
HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
conn.setConnectTimeout(
5000
);
conn.setRequestMethod(
"GET"
);
if
(conn.getResponseCode() ==
200
) {
InputStream instream = conn.getInputStream();
ByteArrayOutputStream outstream =
new
ByteArrayOutputStream();
byte
[] buffer =
new
byte
[
1024
];
while
(instream.read(buffer) != -
1
) {
outstream.write(buffer);
}
instream.close();
return
outstream.toString().trim();
}
return
"连接失败"
;
}
public
static
String post_save(String name, String phone) {
/**
* 出现乱码原因有2个 提交参数没有对中文编码,解决方法:使用URLEncoder.encode(xx,xx)对要提交的汉字转码
* tomatCAT服务器默认采用iso859-1编码,解决方法:把PHP页面保存为UTF-8格式
*/
String path =
"http://192.168.0.117/testxml/web.php"
;
Map<String, String> params =
new
HashMap<String, String>();
try
{
params.put(
"name"
, URLEncoder.encode(name,
"UTF-8"
));
params.put(
"phone"
, phone);
return
sendpostrequest(path, params);
}
catch
(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return
"提交失败"
;
}
private
static
String sendpostrequest(String path,
Map<String, String> params)
throws
Exception {
// name=xx&phone=xx
StringBuilder data =
new
StringBuilder();
if
(params !=
null
&& !params.isEmpty()) {
for
(Map.Entry<String, String> entry : params.entrySet()) {
data.append(entry.getKey()).append(
"="
);
data.append(entry.getValue()).append(
"&"
);
}
data.deleteCharAt(data.length() -
1
);
}
byte
[] entiy = data.toString().getBytes();
// 生成实体数据
URL url =
new
URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(
5000
);
conn.setRequestMethod(
"POST"
);
conn.setDoOutput(
true
);
// 允许对外输出数据
conn.setRequestProperty(
"Content-Type"
,
"application/x-www-form-urlencoded"
);
conn.setRequestProperty(
"Content-Length"
, String.valueOf(entiy.length));
OutputStream outstream = conn.getOutputStream();
outstream.write(entiy);
if
(conn.getResponseCode() ==
200
) {
InputStream instream = conn.getInputStream();
ByteArrayOutputStream byteoutstream =
new
ByteArrayOutputStream();
byte
[] buffer =
new
byte
[
1024
];
while
(instream.read(buffer) != -
1
) {
byteoutstream.write(buffer);
}
instream.close();
return
byteoutstream.toString().trim();
}
return
"提交失败"
;
}
public
static
String httpclient_postsave(String name, String phone) {
/**
* 出现乱码原因有2个 提交参数没有对中文编码,解决方法:使用URLEncoder.encode(xx,xx)对要提交的汉字转码
* tomatCAT服务器默认采用iso859-1编码,解决方法:把PHP页面保存为UTF-8格式
*/
String path =
"http://192.168.0.117/testxml/web.php"
;
Map<String, String> params =
new
HashMap<String, String>();
try
{
params.put(
"name"
, name);
params.put(
"phone"
, phone);
return
sendhttpclient_postrequest(path, params);
}
catch
(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return
"提交失败"
;
}
private
static
String sendhttpclient_postrequest(String path,Map<String, String> params) {
List<NameValuePair> pairs =
new
ArrayList<NameValuePair>();
for
(Map.Entry<String, String> entry : params.entrySet()) {
pairs.add(
new
BasicNameValuePair(entry.getKey(), entry.getValue()));
}
try
{
UrlEncodedFormEntity entity=
new
UrlEncodedFormEntity(pairs,
"UTF-8"
);
HttpPost httpost=
new
HttpPost(path);
httpost.setEntity(entity);
HttpClient client=
new
DefaultHttpClient();
HttpResponse response= client.execute(httpost);
if
(response.getStatusLine().getStatusCode()==
200
){
return
EntityUtils.toString(response.getEntity(),
"utf-8"
);
}
}
catch
(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return
null
;
}
public
static
String httpclient_getsave(String name, String phone) {
/**
* 出现乱码原因有2个 提交参数没有对中文编码,解决方法:使用URLEncoder.encode(xx,xx)对要提交的汉字转码
* tomatCAT服务器默认采用iso859-1编码,解决方法:把PHP页面保存为UTF-8格式
*/
String path =
"http://192.168.0.117/testxml/web.php"
;
Map<String, String> params =
new
HashMap<String, String>();
try
{
params.put(
"name"
, name);
params.put(
"phone"
, phone);
return
sendhttpclient_getrequest(path, params);
}
catch
(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return
"提交失败"
;
}
private
static
String sendhttpclient_getrequest(String path,Map<String, String> map_params) {
List<BasicNameValuePair> params =
new
ArrayList<BasicNameValuePair>();
for
(Map.Entry<String, String> entry : map_params.entrySet()) {
params.add(
new
BasicNameValuePair(entry.getKey(), entry.getValue()));
}
String param=URLEncodedUtils.format(params,
"UTF-8"
);
HttpGet getmethod=
new
HttpGet(path+
"?"
+param);
HttpClient httpclient=
new
DefaultHttpClient();
try
{
HttpResponse response=httpclient.execute(getmethod);
if
(response.getStatusLine().getStatusCode()==
200
){
return
EntityUtils.toString(response.getEntity(),
"utf-8"
);
}
}
catch
(ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return
null
;
}
}
service.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
android:orientation=
"vertical"
>
<TextView
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:text=
"姓名:"
/>
<EditText
android:id=
"@+id/name"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
/>
<TextView
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"电话:"
/>
<EditText
android:id=
"@+id/phone"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
/>
<Button
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"get提交"
android:onClick=
"get_save"
android:layout_marginRight=
"30px"
/>
<Button
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"post提交"
android:onClick=
"post_save"
android:layout_marginRight=
"30px"
/>
<Button
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"Httpclient_post提交"
android:onClick=
"httpclient_postsave"
android:layout_marginRight=
"30px"
/>
<Button
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"Httpclient_get提交"
android:onClick=
"httpclient_getsave"
/>
<TextView
android:id=
"@+id/success"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
/>
</LinearLayout>
main.xml
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<manifest xmlns:android=
"http://schemas.android.com/apk/res/android"
package
=
"caicai.web"
android:versionCode=
"1"
android:versionName=
"1.0"
>
<uses-sdk android:minSdkVersion=
"8"
/>
<uses-permission android:name=
"android.permission.INTERNET"
/>
<application
android:icon=
"@drawable/ic_launcher"
android:label=
"@string/app_name"
>
<activity
android:label=
"@string/app_name"
android:name=
".Cai_webActivity"
>
<intent-filter >
<action android:name=
"android.intent.action.MAIN"
/>
<category android:name=
"android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
AndroidManifest.xml
|