Android HttpResponse - Content has been consumed

前端 未结 5 910
南方客
南方客 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 20:00

    Your get Data() method is perfect and it's working fine i already used this code to check and it's working perfectly for me.

    so might there is a possibility that you called this method twice. if you want to check what i m using check below code i get result perfectly.

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

    And just i m giving the Internet permission only. Still if you not getting this give me your url and params.

提交回复
热议问题