HTTPClient模塊的HttpGet和HttpPost

不管是使用HttpGet,仍是使用HttpPost,都必須經過以下3步來訪問HTTP資源。java

           1.建立HttpGetHttpPost對象,將要請求的URL經過構造方法傳入HttpGetHttpPost對象。
android

           2.使用DefaultHttpClient類的execute方法發送HTTP GETHTTP POST請求,並返回HttpResponse對象。數組

           3.經過HttpResponse接口的getEntity方法返回響應信息,並進行相應的處理。網絡

           若是使用HttpPost方法提交HTTP POST請求,則須要使用HttpPost類的setEntity方法設置請求參數。參數則必須用NameValuePair[]數組存儲。spa

HttpGetcode

public String doGet()
    {
    String uriAPI = "http://XXXXX?str=I+am+get+String";
    String result= "";
//    HttpGet httpRequst = new HttpGet(URI uri);
//    HttpGet httpRequst = new HttpGet(String uri);
//    建立HttpGet或HttpPost對象,將要請求的URL經過構造方法傳入HttpGet或HttpPost對象。
    HttpGet httpRequst = new HttpGet(uriAPI);
//    new DefaultHttpClient().execute(HttpUriRequst requst);
    try {
   //使用DefaultHttpClient類的execute方法發送HTTP GET請求,並返回HttpResponse對象。
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequst);//其中HttpGet是HttpUriRequst的子類
    if(httpResponse.getStatusLine().getStatusCode() == 200)
    {
    HttpEntity httpEntity = httpResponse.getEntity();
    result = EntityUtils.toString(httpEntity);//取出應答字符串
    // 通常來講都要刪除多餘的字符 
    result.replaceAll("\r", "");//去掉返回結果中的"\r"字符,不然會在結果字符串後面顯示一個小方格  
    }
                   else 
                        httpRequst.abort();
           } catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = e.getMessage().toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = e.getMessage().toString();
}
return result;
    }

 HttpPostorm

      若是使用HttpPost方法提交HTTP POST請求,則須要使用HttpPost類的setEntity方法設置請求參數。參數則必須用NameValuePair[]數組存儲。xml

public String doPost()
    {
    String uriAPI = "http://XXXXXX";//Post方式沒有參數在這裏
    String result = "";
    HttpPost httpRequst = new HttpPost(uriAPI);//建立HttpPost對象
    
    List <NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("str", "I am Post String"));
    
    try {
httpRequst.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequst);
    if(httpResponse.getStatusLine().getStatusCode() == 200)
    {
    HttpEntity httpEntity = httpResponse.getEntity();
    result = EntityUtils.toString(httpEntity);//取出應答字符串
    }
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = e.getMessage().toString();
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = e.getMessage().toString();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = e.getMessage().toString();
}
    return result;
    }

以發送鏈接請求時,須要設置連接超時和請求超時等參數,不然會長期中止或者崩潰。對象

  HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, 10*1000);//設置請求超時10秒
    HttpConnectionParams.setSoTimeout(httpParameters, 10*1000); //設置等待數據超時10秒
    HttpConnectionParams.setSocketBufferSize(params, 8192);
    HttpClient httpclient = new DefaultHttpClient(httpParameters); //此時構造DefaultHttpClient時將參數傳入
    因爲是聯網,在AndroidManifest.xml中添加網絡鏈接的權限
    <uses-permission android:name="android.permission.INTERNET"/>
相關文章
相關標籤/搜索