IllegalStateException异常:内容已被消耗probem [英] IllegalStateException: Content has been consumed probem

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

问题描述

我得到了下面的code袭击,因为IllegalStateException的。任何人都可以请帮助我吗? code:

它的一些应用程序通过JSON来获得远程服务器上的数据,并在本地存储插入和列表视图中显示它

 包com.shadatv.shada;进口java.io.BufferedReader中;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;
进口的java.util.ArrayList;
进口java.util.Locale中;进口org.apache.http.HttpEntity;
进口org.apache.http.Htt presponse;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.util.EntityUtils;
进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;进口android.app.ProgressDialog;
进口android.content.Intent;
进口android.content.Shared preferences;
进口android.content.res.Configuration;
进口android.net.ParseException;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.widget.Toast;公共类MediaActivity扩展OptionsMenu {
    区域设置区域设置;    //布尔mybooks;    配置配置=新配置();
    静态共享preferences共享preferenceid;
    静态共享preferences.Editor editorid;
    公共静态最后弦乐MYID =身份识别码;
    串本身份识别码=;
    InputStream为= NULL;
    StringBuilder的SB = NULL;
    串resultt =;
    JSONArray jArray;
    字符串结果= NULL;    公共DAOCanticles cantDatabase = NULL;    INT numberofrowssss;
    INT响应code;    //字符串targetcover,targetbname,targetauthname;    公共字符串caNameField =,caLinkField =,caImgField =;    // /////////////////// ONLINE BESTSALED //////////////////////
    串caNameJson,caLinkJson,caImgJson;    公众的ArrayList<串GT; caNameHolder =新的ArrayList<串GT;();
    公众的ArrayList<串GT; caLinkHolder =新的ArrayList<串GT;();
    公众的ArrayList<串GT; caImgHolder =新的ArrayList<串GT;();    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.media);        cantDatabase =新DAOCanticles(本);
        新LoadData()执行();
    }    // ================================================ =============================    公共无效onClickPrograms(查看视图){
        startActivity(新意图(com.shadatv.ProgramsActivity));
    }    // ================================================ =============================    公共无效onClickCanticles(查看视图){
        startActivity(新意图(com.shadatv.CanticlesActivity));
    }    // ================================================ =============================    公共无效onClickDocumentaries(查看视图){
        startActivity(新意图(com.shadatv.DocumentariesActivity));
    }    // ================================================ =============================    私有类LoadData扩展的AsyncTask<太虚,太虚,太虚> {
        公共ProgressDialog progressDialog1;        @覆盖
        保护无效doInBackground(虚空...... PARAMS){
            cantDatabase.deletetable();
            getCanticlesByJSON();
            addCanticles();
            返回null;
        }        @覆盖
        //这里可以使用UI线程
        在preExecute保护无效(){
            CharSequence的contentTitle =的getString(R.string.loading);
            this.progressDialog1 = ProgressDialog.show(MediaActivity.this,,
                    contentTitle);
        }        // // -------------------------------------------        @覆盖
        保护无效onPostExecute(最终虚空未使用){
            this.progressDialog1.dismiss();
        }
    }    // ================================================ =============================    公共无效getCanticlesByJSON(){
        尝试{
            HttpClient的HttpClient的=新DefaultHttpClient();
            尝试{            }赶上(例外五){
                Log.e(my_log_tag,e.toString());
            }            //缓冲的读者
            尝试{
                HttpPost httppost =新HttpPost(
                        http://dt-works.com/ags/shadatv/canticles/android_data);
                HTT presponse响应= httpclient.execute(httppost);
                HttpEntity实体= response.getEntity();
                是= entity.getContent();
                读者的BufferedReader =新的BufferedReader(
                        新InputStreamReader的(是UTF-8),80);
                SB =新的StringBuilder();
                sb.append(reader.readLine()+\\ n);
                串行=0;
                而((行= reader.readLine())!= NULL){
                    sb.append(行+\\ n);
                }
                is.close();
                结果= EntityUtils.toString(实体);
                //结果= sb.toString();
            }赶上(例外五){
                Log.e(my_log_tag,e.toString());
            }
            尝试{
                结果= result.substring(1);
                jArray =新JSONArray(结果);                JSONObject的json_data = NULL;                的for(int i = 0; I< jArray.length();我++){
                    json_data = jArray.getJSONObject(ⅰ);                    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);                }
            }赶上(JSONException E){
                Log.e(my_log_tag,e.toString());
            }
        }赶上(ParseException的E){
            Log.e(my_log_tag,e.toString());
        }赶上(例外五){
            Log.e(my_log_tag,e.toString());
        }    }    // ================================================ =============================    公共无效addCanticles(){
        尝试{
            的for(int i = 0; I< caNameHolder.size();我++){
                caNameField = caNameHolder.get(ⅰ);
                caLinkField = caLinkHolder.get(ⅰ);
                caImgField = caImgHolder.get(ⅰ);                cantDatabase.createCanticle(caNameField,caLinkField,
                        caImgField);
            }
        }赶上(例外五){
            Log.e(my_log_tag,notfilled尚);
        }
    }
}

的logcat

  7月3日至3日:03:06.565:D / libEGL(6716):加载/system/lib/egl/libEGL_mali.so
7月3日至3日:03:06.570:D / libEGL(6716):加载/system/lib/egl/libGLESv1_CM_mali.so
7月3日至3日:03:06.575:D / libEGL(6716):加载/system/lib/egl/libGLESv2_mali.so
7月3日至3日:03:06.580:D /(6716):设备驱动程序API匹配
7月3日至3日:03:06.580:D /(6716):设备驱动程序API版本:10
7月3日至3日:03:06.580:D /(6716):用户空间API版本:10
7月3日至3日:03:06.580:D /(6716):马里:版本=的Linux r2p4-02rel0 BUILD_DATE =周一3月5日9点47分55秒KST 2012
7月3日至3日:03:06.595:D / OpenGLRenderer(6716):启用调试模式0
7月3日至3日:03:06.655:D /剪贴板(6716):隐藏剪贴板在对话框输入出发:由别人完成了...!
7月3日至3日:03:06.655:W / IInputConnectionWrapper(6716):showStatusIcon上的非活动InputConnection
7月3日至3日:03:15.215:D / dalvikvm(6716):GC_FOR_ALLOC释放9K,免费2%12615K / 12807K,暂停12毫秒
7月3日至3日:03:15.215:I / dalvikvm堆(6716):成长堆(frag的情况下),以15.015MB为2764816字节分配
7月3日至3日:03:15.255:D / dalvikvm(6716):GC_CONCURRENT释放< 1K,免费15315K / 15559K 2%,暂停1毫秒+ 1毫秒
7月3日至3日:03:15.460:D / OpenGLRenderer(6716):刷新缓存(模式0)
7月3日至3日:03:18.945:E / my_log_tag(6716):java.lang.IllegalStateException:内容已被消耗
7月3日至3日:03:18.945:E / my_log_tag(6716):显示java.lang.NullPointerException


解决方案

您已经获得从实体内容,并再次你正设法从实体价值,所以它抛出IllegalStateException异常。
因为你已经消耗从实体内容和你想再次阅读

 结果= EntityUtils.toString(实体); //这引发异常

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

解决方案

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异常:内容已被消耗probem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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