Android HttpResponse - Content has been consumed

前端 未结 5 911
南方客
南方客 2021-01-12 19:19

The method below falls over when reading the HttpResponse with the error: \"Content has been consumed\". I understand that the content can only be consumed once but I get t

5条回答
  •  暖寄归人
    2021-01-12 19:47

    I think your code is right. but try this to access string from HttpEntity: String response_str =EntityUtils.toString(responseEntity, HTTP.UTF_8);

    like i used in my method:

        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;
            }
    

提交回复
热议问题