2014-05-03 36 views
0

我有以下的代碼在Android中使用DefaultHttpClient的http請求,我注意到處理POST和GET請求的區別。例如,通過POST請求,首先將URL直接傳遞給HttpPost,然後完成編碼。但是,通過GET請求,首先完成編碼,然後傳遞帶有參數的url。這是一種必須遵循的規則,還是我可以爲POST和GET執行類似的編碼和URL傳遞順序?提前致謝。傳遞網址到HttpPost和HttpGet

 // check for request method 
     if(method == "POST"){ 
      // request method is POST 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      //encode the post data 
      httpPost.setEntity(new UrlEncodedFormEntity(params)); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 

     }else if(method == "GET"){ 
      // request method is GET 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      String paramString = URLEncodedUtils.format(params, "utf-8"); 
      url += "?" + paramString; 
      HttpGet httpGet = new HttpGet(url); 

      HttpResponse httpResponse = httpClient.execute(httpGet); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 
     } 

回答