1

我因为以下代码中的 IllegalStateException 而感到震惊。有人可以帮我吗?代码:

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

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

日志猫

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
4

1 回答 1

3

您已经从实体中获取了内容,并且您再次尝试从实体中获取值,因此它会引发 IllegalStateException。因为您已经使用了实体中的内容并且您正在尝试再次阅读它

 result = EntityUtils.toString(entity); // this throws the exception
于 2013-03-03T12:54:53.800 回答