Android的http兩種請求方式


http的兩種請求方式:POST和GET

由於Android的SDK包含org.apache.http包,所以不用導入jar了

GET方式:

String serverURL = "http://127.0.0.1/xxx/xx.jsp?username=abc;
HttpGet httpRequest = new HttpGet(serverURL);// 建立http get聯機
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);// 發出http請求
if (httpResponse.getStatusLine().getStatusCode() == 200)
    String result = EntityUtils.toString(httpResponse.getEntity());// 獲取相應的字符串

POST方式:

        String uriAPI = "http://127.0.0.1/xxx/xx.jsp";  //聲明網址字符串
        HttpPost httpRequest = new HttpPost(uriAPI);   //建立HTTP POST聯機
        List <NameValuePair> params = new ArrayList <NameValuePair>();   //Post運作傳送變量必須用NameValuePair[]數組儲存 
        params.add(new BasicNameValuePair("str", "I am Post String"));   
        httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));   //發出http請求
        HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);   //取得http響應
        if(httpResponse.getStatusLine().getStatusCode() == 200)    
          String strResult = EntityUtils.toString(httpResponse.getEntity());   //獲取字符串

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM