2015-11-25 106 views
0

有沒有辦法從HttpClient()GetMethod在java中提取HttpResponse? 我需要使用HttpClient的答覆中的HttpEntity。 任何人都知道一種方式? 我的代碼:從HttpClient獲取httpresponse

client = new HttpClient(); 
GetMethod get = new GetMethod(URL); 
     get.setDoAuthentication(true); 
     try{ 

      int statusCode = client.executeMethod(get); 
      //parsing XML from response 
      //System.out.println(get.getResponseBodyAsString()); 
      if(statusCode == 200){ 
       System.out.println(); 
       // here I need HttpResponse object to get HttpEntity 
      } 


     }finally{ 
      get.releaseConnection(); 
     } 
+0

發表您的完整代碼,請不要那麼快你的問題之前被否決。 –

+0

可能是這個職位將有助於你> http://stackoverflow.com/questions/5769717/how-can-i-get-an-http-response-body-as-a-string-in-java –

+0

否。由於一個客戶可以處理多個請求並因此提供多個響應,因此客戶端與數據或請求之間沒有直接關係。您可以通過URLConnection對象訪問主體。你爲什麼僅限於HttpClient? –

回答

1
client = new HttpClient(); 
GetMethod get = new GetMethod(URL); 
     get.setDoAuthentication(true); 
     try{ 

      HttpResponse response = client.executeMethod(get); 
      //parsing XML from response 
      //System.out.println(get.getResponseBodyAsString()); 
      int statusCode = httpResponse.getStatusLine().getStatusCode(); 
      if(statusCode == 200){ 
       System.out.println(); 
       // here I need HttpResponse object to get HttpEntity 
       BufferedReader buffer = new BufferedReader 
        (new InputStreamReader(response.getEntity().getContent())); 
       String line = ""; 
       while ((line = buffer.readLine()) != null) { 
        textView.append(line); 
       } 
      } 
     } finally { 
      get.releaseConnection(); 
     }