"IllegalStateException:内容已被消耗" [英] "IllegalStateException: Content has been consumed"

查看:59
本文介绍了"IllegalStateException:内容已被消耗"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中,我因IllegalStateException而感到震惊.有人可以帮我吗?代码:

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

这是一个通过json从远程服务器获取数据并将其插入本地存储并在listview中显示的应用程序

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

推荐答案

您已经从实体中获取了内容,并且再次尝试从实体中获取值,因此它抛出IllegalStateException. 因为您已经使用了实体中的内容,并且尝试再次读取

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

这篇关于"IllegalStateException:内容已被消耗"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆