2011-12-06 49 views
1

我寫了一些簡單的應用程序,使http調用(Post)。 的響應可以是選項 之一 - 的Xml - Json的我怎麼能看到我從http post調用回來的json?

我看到(在調試模式下),我從服務器獲取一些反應 - 但我沒有看到任何XML和/或任何JSON格式。

如何獲取xml/json格式?

+0

測試(在你的瀏覽器)的結果,如果你的WS返回一個JSON或沒有,然後 – Houcine

+0

有兩個選擇 - 使用XML或使用JSON。 ..我需要選擇我會打電話的方法 – Yanshof

回答

4

嘗試打印出來的EntityUtils.toString(response.getEntity())

3

GET HTTP響應物體響應然後getEnttity從響應,使用enttity的InputStream的轉換是流進串..

String result=""; 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(url); 
httppost.addHeader("Content-Type", "application/x-www-form-urlencoded"); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8)); 
// Execute HTTP Post Request 
HttpResponse response = httpclient.execute(httppost); 
// get response entity 
HttpEntity entity = response.getEntity(); 
// convert entity response to string 
if (entity != null) { 
      InputStream is = entity.getContent(); 
      // convert stream to string 
      result = convertStreamToString(is); 
      result = result.replace("\n", ""); 
     } 
Log.e("Response from the server:",result) 

而對於convertStreamToString(是);

public static String convertStreamToString(InputStream is) throws Exception { 
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); 
StringBuilder sb = new StringBuilder(); 
String line = null; 
while ((line = reader.readLine()) != null) { 
sb.append(line + "\n"); 
} 
is.close(); 
return sb.toString(); 
} 
相關問題