【问题标题】:IllegalStateException: Content has been consumedIllegalStateException:内容已被使用
【发布时间】:2023-04-10 21:08:01
【问题描述】:

我被以下代码中的IllegalStateException 震惊了。有人可以帮我吗?代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        Button bt = (Button) findViewById(R.id.logbt);
        final EditText user = (EditText) findViewById(R.id.loguser);
        final EditText pw = (EditText) findViewById(R.id.logpw);
        bt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (user.getText().toString() != "" && pw.getText().toString() != "") {
                    Thread t = new Thread() {
                        public void run() {
                            try {
                                HttpClient client = new DefaultHttpClient();
                                String postURL = "http://surfkid.redio.de/login";
                                HttpPost post = new HttpPost(postURL);
                                ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
                                params.add(new BasicNameValuePair("username", user.getText().toString()));
                                params.add(new BasicNameValuePair("password", md5(pw.getText().toString())));
                                UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params, HTTP.UTF_8);
                                post.setEntity(ent);
                                HttpResponse responsePOST = client.execute(post);
                                HttpEntity resEntity = responsePOST.getEntity();
                                final JSONObject jObject = new JSONObject(EntityUtils.toString(resEntity));
                                Log.e("XXX", EntityUtils.toString(resEntity));
                            } catch (Exception e) {
                                Log.e("XXX", e.toString());
                            }
                        }
                    };
                    t.start();
                    // Log.e("XXX",s);
                }
            }
        });
    }

    private String md5(String in) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("MD5");
            digest.reset();
            digest.update(in.getBytes());
            byte[] a = digest.digest();
            int len = a.length;
            StringBuilder sb = new StringBuilder(len << 1);
            for (int i = 0; i < len; i++) {
                sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
                sb.append(Character.forDigit(a[i] & 0x0f, 16));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Logcat 消息:

01-18 18:39:53.383: 错误/XXX(7113): java.lang.IllegalStateException: 内容已被消费

【问题讨论】:

  • 什么“不起作用”?
  • 您是否在控制台窗口旁边看到了 Logcat,如果您没有看到,请单击 Windows 菜单,然后单击其他并选择 Logcat,并告诉我们您在显示 logcat 时是否有问题或不,请快点
  • 在记录器中没有出现带有XXX的日志
  • IllegalStateException: 内容已被消费
  • 因为你注释掉了 :)

标签: android


【解决方案1】:

您只能从Entity 一次消费Content

在行中:

final JSONObject jObject = new JSONObject(EntityUtils.toString(resEntity));

您已经消费了内容,并且在这里再次使用相同的内容:

Log.e("XXX",EntityUtils.toString(resEntity));

这就是它导致IllegalStateException: Content has been consumed的原因

所以解决方法就在这里:

String _response=EntityUtils.toString(resEntity); // content will be consume only once
final JSONObject jObject=new JSONObject(_response);
Log.e("XXX",_response);

【讨论】:

  • 非常感谢.. 这是非常有用的信息.. httptresponse 之后的内容消费只能执行一次.. 我也犯了同样的错误,但现在完美了。非常感谢。
  • 非常感谢...救了我:)
【解决方案2】:

如果您在调试器的表达式中编写消费语句,也会发生这种情况!!!
(例如,如果您正在“监视”EntityUtils.toString(resEntity) 之类的东西)

【讨论】:

    【解决方案3】:

    首先,这一定是每个新的 android 程序员都会犯的错误,并且每天都会在这里被问到。你有

    user.getText().toString()!= ""&& pw.getText().toString()!= ""
    

    这并没有达到你想要的效果。你需要

     !user.getText().toString().equals("")&& !pw.getText().toString().equals("")
    

    另外,您需要打印堆栈跟踪。在您的例外中,您需要

    e.printStackTrace()
    

    而不是记录

    e.toString()
    

    【讨论】:

    • 顺便说一句,在这个离题的话题上,我更喜欢Log.e(TAG, "Error", exception) 而不是exception.toString()
    • 任何人都可以编译那个类(user:test pw:sers)
    【解决方案4】:

    我刚刚处理了一个对实体进行空检查导致它被标记为“已使用”的情况。希望我的头痛能帮助其他人。

    Vikas Patidar 的回答帮助我找出了谜语的关键,非常感谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      • 2012-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-25
      • 1970-01-01
      相关资源
      最近更新 更多