分享

android http get post

 昵称31502522 2016-03-14

要使用HttpClient,需要了解一些类:

1、ClientConnectionManager接口:该接口是客户端连接管理器接口,主要提供以下几个抽象方法:

ClientConnectionManager(关闭所有无效超时的连接)、closeIdleConnection(关闭空闲的连接)、releaseConnection(释放一个连接)、requestConnection(请求一个新的连接)、shutdown(关闭管理器并且释放资源)

2、DefaultHttpClient:一个默认的Http客户端,我们可以使用它来创建一个Http连接,代码如下:

[java] view plain copy
  1. HttpClient httpClient = new DefaultHttpClient();  

3、HttpResponse:是一个Http连接相应,当执行一个Http连接后,就会返回一个HttpResponse,可以通过HttpResponse获得一些响应信息。下面是一个请求Http连接并且获得该请求是否成功的代码:

[java] view plain copy
  1. HttpResponse httpResponse = httpClient.execute(httpGet);  
  2.             if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){  
  3.                 //连接成功  
  4.             }  

通过上面几个类的连接,下面将分别使用Get和Post方式请求一个网页。

其中有两个资源文件,两个jsp的内容分别如下:
http.jsp
[html] view plain copy
  1. <html>  
  2.   <head>   
  3.     <title>My JSP 'index.jsp' starting page</title>  
  4.   </head>  
  5.   <body>  
  6.     <%  
  7.         out.println("<h1>HTTP TEST<br/>http test</h1>");  
  8.      %>  
  9.   </body>  
  10. </html>  
httpGet.jsp
[html] view plain copy
  1. <html>  
  2.   <head>  
  3.     <title>My JSP 'index.jsp' starting page</title>  
  4.   </head>   
  5.   <body>  
  6.     <%  
  7.         String type = request.getParameter("par");  
  8.         String result = new String(type.getBytes("iso-8859-1"),"gb2312");  
  9.         out.println("<h1>parameters:"+result+"</h1>");  
  10.      %>  
  11.   </body>  
  12. </html>  

先看看HttpClient中如何使用Get方式获取数据,这里需要使用HttpGet来构建一个Get方式的Http请求,然后通过HttpClient来执行这个请求,HttpResponse在收到这个请求后给出响应,最后通过“httpResponse.getStatusLine().getStatusCode()”来判断请求是否成功,并且处理,具体代码如下:

[java] view plain copy
  1. public class GetActivity extends Activity {  
  2.     @Override  
  3.     protected void onCreate(Bundle savedInstanceState) {  
  4.         // TODO Auto-generated method stub  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.http);  
  7.         TextView textView = (TextView)findViewById(R.id.text);  
  8.           
  9.         String httpUrl = "http://59.64.158.106:8080/test/http.jsp";  
  10.         //HttpGet对象  
  11.         HttpGet httpGet = new HttpGet(httpUrl);  
  12.         try{  
  13.             //取得HttpClient对象  
  14.             HttpClient httpClient = new DefaultHttpClient();  
  15.             //请求HttpClient,取得HttpResponse  
  16.             HttpResponse httpResponse = httpClient.execute(httpGet);  
  17.             //请求成功  
  18.             if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){  
  19.                 //取得返回的字符串  
  20.                 String strResult = EntityUtils.toString(httpResponse.getEntity());  
  21.                 textView.setText(strResult);  
  22.             }else{  
  23.                 textView.setText("请求错误");  
  24.             }  
  25.         }catch (ClientProtocolException e) {  
  26.             // TODO: handle exception  
  27.             Log.e("GetActivity""ClientProtocolException");  
  28.             e.printStackTrace();  
  29.         }catch (IOException e) {  
  30.             // TODO: handle exception  
  31.             Log.e("GetActivity""IOException");  
  32.             e.printStackTrace();  
  33.         }  
  34.     }  
  35. }  

Post方法则比Get方法稍微复杂一点。首先使用HttpPost来构建一个Post方式的请求,然后需要使用NameValuePair来保存要传递的参数,还需要设置所使用的字符集,最后就和Get方式一样通过HttpClient来请求这个链接,返回响应并且处理,下面是一个例子:

[java] view plain copy
  1. public class PostActivity extends Activity {  
  2.     @Override  
  3.     protected void onCreate(Bundle savedInstanceState) {  
  4.         super.onCreate(savedInstanceState);  
  5.         setContentView(R.layout.http);  
  6.         TextView textView = (TextView)findViewById(R.id.text);  
  7.         //http地址  
  8.         String httpUrl = "http://59.64.158.106:8080/test/httpGet.jsp";  
  9.         //httpPost连接对象  
  10.         HttpPost httpPost = new HttpPost(httpUrl);  
  11.         //使用NameValuePair来保存要传递的post阐述  
  12.         List<NameValuePair> params = new ArrayList<NameValuePair>();  
  13.         //添加要传递的参数  
  14.         params.add(new BasicNameValuePair("par""HttpClient_android_post"));  
  15.         try{  
  16.             //设置字符集  
  17.             HttpEntity httpEntity = new UrlEncodedFormEntity(params, "gb2312");  
  18.             httpPost.setEntity(httpEntity);  
  19.             //取得默认的HttpClient  
  20.             HttpClient httpClient = new DefaultHttpClient();  
  21.             //取得HttpResponse  
  22.             HttpResponse httpResponse = httpClient.execute(httpPost);  
  23.             //HttpStatus.SC_OK)表示连接成功  
  24.             if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){  
  25.                 //取得返回的字符串  
  26.                 String result = EntityUtils.toString(httpResponse.getEntity());  
  27.                 textView.setText(result);  
  28.             }else{  
  29.                 textView.setText("请求错误");  
  30.             }  
  31.         }catch (ClientProtocolException e) {  
  32.             Log.e("PostActivity""ClientProtocolException");  
  33.             e.printStackTrace();  
  34.         }catch (IOException e) {  
  35.             Log.e("PostActivity""IOException");  
  36.             e.printStackTrace();  
  37.         }  
  38.     }  
  39. }  

注意:代码中的url地址中的ip:127.0.0.1需要修改成自己所需要的地址

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多