Android HttpClient详解及调用示例

2019-12-10 19:59:55王旭
  • // TODO Auto-generated catch block   e.printStackTrace();  
  • } catch (IOException e) {   // TODO Auto-generated catch block  
  • e.printStackTrace();   } 

    2. POST

     

     
    1. //和GET方式一样,先将参数放入List   params = new LinkedList<BasicNameValuePair>();  
    2. params.add(new BasicNameValuePair("param1", "Post方法"));   params.add(new BasicNameValuePair("param2", "第二个参数"));  
    3. try {   HttpPost postMethod = new HttpPost(baseUrl);  
    4. postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //将参数填入POST Entity中   HttpResponse response = httpClient.execute(postMethod); //执行POST方法  
    5. Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码   Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //获取响应内容  
    6. } catch (UnsupportedEncodingException e) {   // TODO Auto-generated catch block  
    7. e.printStackTrace();   } catch (ClientProtocolException e) {  
    8. // TODO Auto-generated catch block   e.printStackTrace();  
    9. } catch (IOException e) {   // TODO Auto-generated catch block