以下方法在读取HttpResponse时会出现错误:“已消耗内容”。我知道内容只能被消耗一次,但是我在第一次尝试时遇到了这个错误,我没有在代码中看到我可能会消耗它两次。
private static String getData(String url, HttpParams params) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
if (params != null) {
httpGet.setParams(params);
}
String result = "";
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
content.close();
result = builder.toString();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
答案 0 :(得分:6)
确保您在Eclipse观看视图中没有类似 http_response.getEntity()的内容 如果你这样做,那么这就是消耗你的流......
答案 1 :(得分:2)
这是在模拟器中还是在手机上?它可能是模拟器特定的问题。我在我的设备上测试它,它完美无缺。
您是否有可能正在使用内容的调试器手表?
答案 2 :(得分:2)
如果您多次使用该实体,可能会发生这种情况,与此类似:
EntityUtils.toString(entity, HTTP.UTF_8))
答案 3 :(得分:1)
您的获取Data()方法非常完美且工作正常我已经使用此代码进行检查,并且它对我来说非常适合。
所以可能有两次调用此方法的可能性。如果你想检查我在下面使用的检查代码,我会得到完美的结果。
package com.sandeeppatel.httpget;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
public class HttpGetActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.vogella.com");
/*if (params != null) {
httpGet.setParams(params);
}*/
String result = "";
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
content.close();
result = builder.toString();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
// return result;
}
}
我只是在网上获得许可。 如果你没有得到这个,请给我你的网址和参数。
答案 4 :(得分:1)
我认为你的代码是对的。但尝试这个从HttpEntity访问字符串: String response_str = EntityUtils.toString(responseEntity,HTTP.UTF_8);
像我在我的方法中使用的那样: public String SetObjectSecurity(String username, String password,
String clientName,String docRid,String ObjectRidsForCheckSum) throws JSONException, ClientProtocolException, IOException
{
String SetObjectSecurityURL = "url";
StringEntity str_request_entity = null;
HttpResponse http_response = null;
HttpGet getrequest = new HttpGet(SetObjectSecurityURL);
postrequest.setHeader("Accept", "application/json");
postrequest.setHeader("Content-type", "application/json");
//set param here
HttpClient httpClient = new DefaultHttpClient();
http_response = httpClient.execute(getrequest);
//Log.e("Status code ",http_response);
HttpEntity responseEntity = http_response.getEntity();
String response_str =EntityUtils.toString(responseEntity, HTTP.UTF_8);
Log.e("output",response_str);
int i = http_response.getStatusLine().getStatusCode();
Log.e("status","code "+i);
if(i==this){
do this}
else
{
this
}
return response_str;
}