eng.ahmed
eng.ahmed

Reputation: 925

"IllegalStateException: Content has been consumed"

I got struck because of IllegalStateException in the following code. Can anybody please help me? Code:

it's some app to get data from remote server by json and insert it at local storage and display it in listview

package com.shadatv.shada;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Locale;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MediaActivity extends OptionsMenu {
    Locale locale;

    // boolean mybooks;

    Configuration config = new Configuration();
    static SharedPreferences sharedPreferenceid;
    static SharedPreferences.Editor editorid;
    public static final String Myid = "Myid";
    String myid = "";
    InputStream is = null;
    StringBuilder sb = null;
    String resultt = "";
    JSONArray jArray;
    String result = null;

    public DAOCanticles cantDatabase = null;

    int numberofrowssss;
    int responseCode;

    //String targetcover, targetbname, targetauthname;

    public String caNameField = "", caLinkField = "", caImgField = "";

    // ///////////////////ONLINE BESTSALED//////////////////////
    String caNameJson, caLinkJson, caImgJson;

    public ArrayList<String> caNameHolder = new ArrayList<String>();
    public ArrayList<String> caLinkHolder = new ArrayList<String>();
    public ArrayList<String> caImgHolder = new ArrayList<String>();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.media);

        cantDatabase = new DAOCanticles(this);
        new LoadData().execute();
    }

    // =============================================================================

    public void onClickPrograms(View view) {
        startActivity(new Intent("com.shadatv.ProgramsActivity"));
    }

    // =============================================================================

    public void onClickCanticles(View view) {
        startActivity(new Intent("com.shadatv.CanticlesActivity"));
    }

    // =============================================================================

    public void onClickDocumentaries(View view) {
        startActivity(new Intent("com.shadatv.DocumentariesActivity"));
    }

    // =============================================================================

    private class LoadData extends AsyncTask<Void, Void, Void> {
        public ProgressDialog progressDialog1;

        @Override
        protected Void doInBackground(Void... params) {
            cantDatabase.deletetable();
            getCanticlesByJSON();
            addCanticles();
            return null;
        }

        @Override
        // can use UI thread here
        protected void onPreExecute() {
            CharSequence contentTitle = getString(R.string.loading);
            this.progressDialog1 = ProgressDialog.show(MediaActivity.this, "",
                    contentTitle);
        }

        // -------------------------------------------//

        @Override
        protected void onPostExecute(final Void unused) {
            this.progressDialog1.dismiss();
        }
    }

    // =============================================================================

    public void getCanticlesByJSON() {
        try {
            HttpClient httpclient = new DefaultHttpClient();
            try {

            } catch (Exception e) {
                Log.e("my_log_tag", e.toString());
            }

            // buffered reader
            try {
                HttpPost httppost = new HttpPost(
                        "http://dt-works.com/ags/shadatv/canticles/android_data");
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(is, "UTF-8"), 80);
                sb = new StringBuilder();
                sb.append(reader.readLine() + "\n");
                String line = "0";
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                result = EntityUtils.toString(entity);
                //result = sb.toString();
            } catch (Exception e) {
                Log.e("my_log_tag", e.toString());
            }
            try {
                result = result.substring(1);
                jArray = new JSONArray(result);

                JSONObject json_data = null;

                for (int i = 0; i < jArray.length(); i++) {
                    json_data = jArray.getJSONObject(i);

                    caNameJson = json_data.getString("ca_name");
                    Toast.makeText(MediaActivity.this, caNameJson, Toast.LENGTH_LONG).show();
                    caLinkJson = json_data.getString("ca_link");
                    caImgJson = json_data.getString("ca_link");

                    caNameHolder.add(caNameJson);
                    caLinkHolder.add(caLinkJson);
                    caImgHolder.add(caImgJson);

                }
            } catch (JSONException e) {
                Log.e("my_log_tag", e.toString());
            }
        } catch (ParseException e) {
            Log.e("my_log_tag", e.toString());
        } catch (Exception e) {
            Log.e("my_log_tag", e.toString());
        }

    }

    // =============================================================================

    public void addCanticles() {
        try {
            for (int i = 0; i < caNameHolder.size(); i++) {
                caNameField = caNameHolder.get(i);
                caLinkField = caLinkHolder.get(i);
                caImgField = caImgHolder.get(i);

                cantDatabase.createCanticle(caNameField, caLinkField,
                        caImgField);
            }
        } catch (Exception e) {
            Log.e("my_log_tag", "notfilled yet");
        }
    }
}

logcat

03-03 07:03:06.565: D/libEGL(6716): loaded /system/lib/egl/libEGL_mali.so
03-03 07:03:06.570: D/libEGL(6716): loaded /system/lib/egl/libGLESv1_CM_mali.so
03-03 07:03:06.575: D/libEGL(6716): loaded /system/lib/egl/libGLESv2_mali.so
03-03 07:03:06.580: D/(6716): Device driver API match
03-03 07:03:06.580: D/(6716): Device driver API version: 10
03-03 07:03:06.580: D/(6716): User space API version: 10 
03-03 07:03:06.580: D/(6716): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Mon Mar  5 09:47:55 KST 2012 
03-03 07:03:06.595: D/OpenGLRenderer(6716): Enabling debug mode 0
03-03 07:03:06.655: D/CLIPBOARD(6716): Hide Clipboard dialog at Starting input: finished by someone else... !
03-03 07:03:06.655: W/IInputConnectionWrapper(6716): showStatusIcon on inactive InputConnection
03-03 07:03:15.215: D/dalvikvm(6716): GC_FOR_ALLOC freed 9K, 2% free 12615K/12807K, paused 12ms
03-03 07:03:15.215: I/dalvikvm-heap(6716): Grow heap (frag case) to 15.015MB for 2764816-byte allocation
03-03 07:03:15.255: D/dalvikvm(6716): GC_CONCURRENT freed <1K, 2% free 15315K/15559K, paused 1ms+1ms
03-03 07:03:15.460: D/OpenGLRenderer(6716): Flushing caches (mode 0)
03-03 07:03:18.945: E/my_log_tag(6716): java.lang.IllegalStateException: Content has been consumed
03-03 07:03:18.945: E/my_log_tag(6716): java.lang.NullPointerException

Upvotes: 1

Views: 6777

Answers (1)

Pragnani
Pragnani

Reputation: 20155

You've already get the content from the entity and again you are trying to get the value from the entity, so it throws IllegalStateException. Because you've already consume the content from the entity and you are trying to read it again

 result = EntityUtils.toString(entity); // this throws the exception

Upvotes: 3

Related Questions