简单的网页下载,HttpURLConnection能够完成,可是涉及到用户登陆等权限相关问题,就须要涉及Session、Cookies。,就很难使用HttpURLConnection来处理了。Apache开源组织提供了一个HttpClient项目能够处理这些问题。HttpClient关注于如何发送请求、接受请求,以及管理HTTP连接。
使用HttpClient对象来发送请求、接受响应步骤:php
建立HttpClient对象web
若是要发送GET请求,建立HttpGet对象;若是是POST请求,则建立HttpPost对象。服务器
若是须要添加参数,对于HttpGet直接在构造URL的时候填入参数。对于POST请求,使用setEntity(HttpEntity entity)方法来设置网络
调用HttpClient对象的execute(HttpUriRequest request)发送请求,此方法返回一个HttpResponseide
调用HttpResponse的getALLHeaders()、getHeaders(String name)等方法可获取服务器响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器响应内容。post
注意:ui
很多地方说能够使用HttpGet和HttpPost共同的setParams(HttpParams params)方法添加请求参数,可是我没有设置成功,网上搜索发现好多人也没成功。Even Apache’s official example uses URIBuilder’s setParameter method to build the params out in the URI,因此没有使用这种方法.this
GET请求Demo:url
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
|
public
class
MainActivity
extends
Activity {
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textViewShow = (TextView) findViewById(R.id.showText);
//直接在URL后添加请求参数
try
{
// 建立DefaultHttpClient对象
HttpClient httpclient =
new
DefaultHttpClient();
// 建立一个HttpGet对象
HttpGet get =
new
HttpGet(url);
// 获取HttpResponse对象
HttpResponse response = httpclient.execute(get);
//判断是否连接成功
if
(response.getStatusLine().getStatusCode() ==
200
) {
//实体转换为字符串
String content = EntityUtils.toString(response.getEntity());
textViewShow.setText(content);
}
else
{
textViewShow.setText(
"网络错误"
);
}
}
catch
(ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return
true
;
}
}
|
POST请求Demo:spa
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
|
public
class
MainActivity
extends
Activity {
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textViewShow = (TextView) findViewById(R.id.showText);
HttpClient httpClient =
new
DefaultHttpClient();
try
{
HttpPost post =
new
HttpPost(url);
List params =
new
ArrayList();
params.add(
new
BasicNameValuePair(
"get1"
,
"hello"
));
params.add(
new
BasicNameValuePair(
"get2"
,
"usrl"
));
post.setEntity(
new
UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse response = httpClient.execute(post);
if
(response.getStatusLine().getStatusCode() ==
200
){
String content = EntityUtils.toString(response.getEntity());
textViewShow.setText(content);
}
else
{
textViewShow.setText(
"网络问题"
);
}
}
catch
(UnsupportedEncodingException e) {
// TODO Auto-generated catch block
textViewShow.setText(
"UnsupportedEncodingException"
);
}
catch
(ClientProtocolException e) {
// TODO Auto-generated catch block
textViewShow.setText(
"ClientProtocolException"
);
}
catch
(IOException e) {
// TODO Auto-generated catch block
textViewShow.setText(
"IOException"
);
}
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return
true
;
}
}
|