android 5.1 httpclient,【Android进阶学习】Http编程之HttpClient

在Android开发中,Android SDK附带了Apache的HttpClient,它是一个完善的客户端。它提供了对HTTP协议的全面支持,可以使用HttpClient的对象来执行HTTP GET和HTTP POST调用。

HTTP工作原理:

1.客户端(一般是指浏览器,这里是指自己写的程序)与服务器建立连接

2.建立连接后,客户端向服务器发送请求

3.服务器接收到请求后,向客户端发送响应信息

4.客户端与服务器断开连接

HttpClient的一般使用步骤:

1.使用DefaultHttpClient类实例化HttpClient对象

2.创建HttpGet或HttpPost对象,将要请求的URL通过构造方法传入HttpGet或HttpPost对象。

3.调用execute方法发送HTTP GET或HTTP POST请求,并返回HttpResponse对象。

4.通过HttpResponse接口的getEntity方法返回响应信息,并进行相应的处理。

最后记得要在AndroidManifest.xml文件添加网络权限

下面是具体的例子:

1.使用HttpClient来执行GET调用

在LogCat窗口就能看到输出的信息

packagecom.lingdududu.http;

importjava.io.InputStream;

importorg.apache.http.HttpResponse;

importorg.apache.http.HttpStatus;

importorg.apache.http.client.HttpClient;

importorg.apache.http.client.methods.HttpGet;

importorg.apache.http.impl.client.DefaultHttpClient;

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.util.Log;

publicclassHttpGetActivityextendsActivity {

String uri ="http://developer.android.com/";

finalString TAG_STRING ="TAG";

@Override

publicvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

try{

//得到HttpClient对象

HttpClient getClient =newDefaultHttpClient();

//得到HttpGet对象

HttpGet request =newHttpGet(uri);

//客户端使用GET方式执行请教,获得服务器端的回应response

HttpResponse response = getClient.execute(request);

//判断请求是否成功

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

Log.i(TAG_STRING,"请求服务器端成功");

//获得输入流

InputStream  inStrem = response.getEntity().getContent();

intresult = inStrem.read();

while(result != -1){

System.out.print((char)result);

result = inStrem.read();

}

//关闭输入流

inStrem.close();

}else{

Log.i(TAG_STRING,"请求服务器端失败");

}

}catch(Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

使用HTTP GET调用有一个缺点就是,请求的参数作为URL一部分来传递,以这种方式传递的时候,URL的长度应该在2048个字符之内。如果超出这个这范围,就要使用到HTTP POST调用。

2.使用HttpClient来执行POST调用

使用POST调用进行参数传递时,需要使用NameValuePair来保存要传递的参数。NameValuePair封装了一个键/值组合。另外,还需要设置所使用的字符集。

packagecom.androidbook.services.httppost;

importjava.io.BufferedReader;

importjava.io.IOException;

importjava.io.InputStreamReader;

importjava.util.ArrayList;

importjava.util.List;

importorg.apache.http.HttpResponse;

importorg.apache.http.NameValuePair;

importorg.apache.http.client.HttpClient;

importorg.apache.http.client.entity.UrlEncodedFormEntity;

importorg.apache.http.client.methods.HttpPost;

importorg.apache.http.impl.client.DefaultHttpClient;

importorg.apache.http.message.BasicNameValuePair;

importandroid.app.Activity;

importandroid.os.Bundle;

publicclassHttpPostActivityextendsActivity {

String uri ="http://developer.android.com/";

@Override

publicvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

BufferedReader in =null;

try{

HttpClient client =newDefaultHttpClient();

HttpPost request =newHttpPost("http://code.google.com/android/");

//使用NameValuePair来保存要传递的Post参数

List postParameters =newArrayList();

//添加要传递的参数

postParameters.add(newBasicNameValuePair("id","12345"));

postParameters.add(newBasicNameValuePair("username","dave"));

//实例化UrlEncodedFormEntity对象

UrlEncodedFormEntity formEntity =newUrlEncodedFormEntity(

postParameters);

//使用HttpPost对象来设置UrlEncodedFormEntity的Entity

request.setEntity(formEntity);

HttpResponse response = client.execute(request);

in =newBufferedReader(

newInputStreamReader(

response.getEntity().getContent()));

StringBuffer string =newStringBuffer("");

String lineStr ="";

while((lineStr = in.readLine()) !=null) {

string.append(lineStr +"\n");

}

in.close();

String resultStr = string.toString();

System.out.println(resultStr);

}catch(Exception e) {

// Do something about exceptions

}finally{

if(in !=null) {

try{

in.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值