我已经做到了这一点:
response = httpclient.execute(targetHost, httppost);
if(response.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = response.getEntity();
System.out.println("Entity:"+entity);
if (entity != null)
{
String responseBody = EntityUtils.toString(entity);
System.out.println("finalResult"+responseBody.toString());
}
关于它的事情是,第一println()
显示这一点: org.apache.http.conn.BasicManagedEntity@481e8150
这是很好的。
但是,第二个System.out.println("finalResult"+responseBody.toString());
只显示该finalResult
。 那么,什么是错的:
String responseBody = EntityUtils.toString(entity);
System.out.println("finalResult"+responseBody.toString());
???
重要事项本HttpEntity entity = response.getEntity();
等于org.apache.http.conn.BasicManagedEntity@481e8150
。 所以这个问题必须在这里:
串responseBody = EntityUtils.toString(实体);.
请帮忙!!!
首先,看看你的服务器未返回空响应:
response.getEntity().getContentLength(); //it should not be 0
二,请尝试以下,以响应转换成字符串:
StringBuilder sb = new StringBuilder();
try {
BufferedReader reader =
new BufferedReader(new InputStreamReader(entity.getContent()), 65728);
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
}
catch (IOException e) { e.printStackTrace(); }
catch (Exception e) { e.printStackTrace(); }
System.out.println("finalResult " + sb.toString());
您可以使用此之一:
String s = EntityUtils.toString(httpRes.getEntity());
org.apache.http.conn.BasicManagedEntity@f8a5dec
反应过来的时候,我们直接打印HttpEntity对象。 例如:
HttpEntity httpEntity=httpResponse.getEntity();
现在对于从服务器获取的实际响应我们需要做以下步骤:
public String convertStreamtoString(InputStream is){
String line="";
String data="";
try{
BufferedReader br=new BufferedReader(new InputStreamReader(is));
while((line=br.readLine())!=null){
data+=line;
}
}
catch(Exception e){
e.printStackTrace();
}
return data;
}
只要调用上述方法并传递httpEntity作为参数。 请享用!!
试试这个:
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String body = "";
while ((body = rd.readLine()) != null)
{
Log.e("HttpResponse", body);
}
试试这个 :
HttpEntity entity = response.getEntity();
final String content;
try
{
content = EntityUtils.toString(entity);
runOnUiThread(new Runnable()
{
@Override
public void run()
{
webView.loadData(content, "text/html", "UTF-8");
}
});
}
试试这个
BufferedReader in = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
//SB to make a string out of the inputstream
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
//the json string is stored here
String result = sb.toString();