2-7. HttpClient

来源:互联网 发布:3cdaemon syslog 端口 编辑:程序博客网 时间:2024/05/16 07:43

                                                   HttpClient   (比HttpUriConnetion简化,将GET封装成了HttpGet类,POST封装成了HttpPost类)

(1)GET方法

1.新建URL

-----String url="http://5billion.com.cn/post.php?name=abcd";

2.新建GET型请求

----HttpGet.HttpGet(String url);

3.新建Http客户端

-----DefaultHttpClient.DefaultHttpClient();

4.是用客户端执行请求

-----HttpClient.execute(HttpUriRequest request) throws IOException,ClientProtocolException//该方法的返回值是HttpResponse性,即相应

5.处理相应:得到HttpResponse类型的响应后,网络上的工作就完成了。接下来就是在本地进行的处理了

----HttpResponse.getStatusLine();//得到状态行

-----HttpResponse.gerEntity();//得到结果

(2)POST方法:与GET不同之处就是参数的传递,POST中传递参数需要使用NaneValuePair类,即键值对

1.新建HttpPost型请求

----HttpPost.HttpPost(String url);

2.新建保存参数的数据结构

----2.1新建一个列表保存键值对

     List<NameValuePair> list=new ArrayList<NameValuePair>();

----2.2新建真正的数据,以键值对的形式保存

     BasicNameValuePair.BasicNameValuePair(String name,String value);

-----2.3将键值对添加到列表中

     list.add(NameValuePair object);

3.为参数设置编码方式:第一个参数编码参数,第二个参数编码方式

----UrlEncodeFormEntity.UrlEncodeFormEntity(List<?extends NameValuePair> list,String encotiong) throws UnsupportedEncodingException

4.讲参数添加到请求中

-----HttpEntityEnclosingRequestBase.setEntity(HttpEntity entity);

5.新建Http客户端

-----DefaultHttpClient.DefaultHttpClient();

6.使用客户端执行请求

HttpClient.execute(HttpUriRequest request) throws IOException,ClientProtocolException//该方法的返回值是HttpResponse性,即相应

7.处理相应

String result=EntityUtils.toString(httpResponse.getEntity());

tv.setText(result);

实例:

(2-1)新建项目HttpClient,在HttpClientActivity中定义一个按钮,设置监听,以发送GET请求,获取网络资源
try{
          //通过ip地址实例化HttpGet对象
    HttpGet httpGet=new HttpGet("
http://www.baidu.com");
         //实例化HttpClient接口
   HttpClient httpClient=new DefaultHttpClient();
      //声明HttpResponse对象
   HttpResponse hResponse;
     //执行GET请求---将HttpResponse,HttpGet,HttpClent联系起来
hResponse=hpptClient.execute(httpGet);
/ /连接成功
if(hResponse.getStatusLine().getStatusCode==200){
      //得到HttpEntity对象并将其转化为String类型
String str=EntityUtils.toString(hResponse.getEntity());
    //显示HttpEntity
getTextView=(TextView)findViewById(R.id.textView1);
getTextView.setText(str);
}}catch(ClientProtocolException e){}
  catch(IOException e){}
  发现:一共牵扯到了
  HttpClient接口--http客户端接口;
  HttpResponse接口(getEntity(),getStatusLine())--http响应接口;
  StatusLine接口--http状态行,getStatusCode()方法返回(200:服务器成功返回网页;404:请求的网页不存在;503:服务不可用);
  HttpEntity接口--http消息发送和接受的实体;
  HttpGet类--实现HttpRequest接口和 HttpUriRequest接口;
  HttpPost类--也实现HttpRequest接口和 HttpUriRequest接口;
  NameValuePair接口--用于简单的封装名值对,只有getName(),getValue()方法
(2-2)在onCreate()方法中添加如下代码,防止抛出网络异常
//设置线程政策
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskWrites()
.detectDiskReaders()
.detectNetwork()
.penaltyLog()
.build());
//设置虚拟内存政策
StrictMode.setVmPolicy(new StrickMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.penaltyLog()
.penaltyDeath()
.build());

(2-3)在AndroidMenifest.xml文件中添加用户权限,运行应用程序访问网络
<uses-permission:name="android.permission.INTERNET"/>

(2.1-4)运行结果

原创粉丝点击