我怎么能看到我从http post电话回来的json?

时间:2011-12-06 09:08:21

标签: android

我写了一些简单的应用程序,使http调用(Post)。 响应可以是选项之一            - Xml            - 杰森

我看到(在调试模式下)我从服务器得到一些响应 - 但我没有看到任何xml或/和任何json格式。

我如何获得xml / json格式?

2 个答案:

答案 0 :(得分:4)

尝试打印出EntityUtils.toString(response.getEntity())

的结果

答案 1 :(得分:3)

在Response对象中获取Http Response,然后从响应中获取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();
}