本文介绍了IllegalStateException异常:内容已被消耗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到了下面的code打,因为 IllegalStateException异常
。任何人都可以请帮我吗? code:
进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;
进口java.security.MessageDigest中;
进口java.security.NoSuchAlgorithmException;
进口的java.util.ArrayList;
进口的java.util.List;
进口org.apache.http.HttpEntity;
进口org.apache.http.Htt presponse;
进口org.apache.http.NameValuePair;
进口org.apache.http.ParseException;
进口org.apache.http.client.ClientProtocolException;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.entity.UrlEn codedFormEntity;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.message.BasicNameValuePair;
进口org.apache.http.protocol.HTTP;
进口org.apache.http.util.EntityUtils;
进口org.json.JSONObject;
进口android.app.Activity;
进口android.os.Bundle;
进口android.telephony.gsm.GsmCellLocation;
进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.Toast;
公共类登录扩展活动{
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.login);
按钮BT =(按钮)findViewById(R.id.logbt);
最终用户的EditText =(EditText上)findViewById(R.id.loguser);
最后的EditText PW =(EditText上)findViewById(R.id.logpw);
bt.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
如果(user.getText()的toString()=!&放大器;&安培;!pw.getText()的toString()=){
线程t =新的Thread(){
公共无效的run(){
尝试 {
HttpClient的客户端=新DefaultHttpClient();
字符串postURL =http://surfkid.redio.de/login;
HttpPost后=新HttpPost(postURL);
ArrayList的<的NameValuePair> PARAMS =新的ArrayList<的NameValuePair>();
params.add(新BasicNameValuePair(用户名,user.getText()的toString()));
params.add(新BasicNameValuePair(密码,MD5(pw.getText()的toString())));
UrlEn codedFormEntity ENT =新UrlEn codedFormEntity(参数,可以HTTP.UTF_8);
post.setEntity(ENT);
HTT presponse responsePOST = client.execute(后);
HttpEntity resEntity = responsePOST.getEntity();
最后的JSONObject jObject =新的JSONObject(EntityUtils.toString(resEntity));
Log.e(XXX,EntityUtils.toString(resEntity));
}赶上(例外五){
Log.e(XXX,e.toString());
}
}
};
t.start();
// Log.e(XXX,多个);
}
}
});
}
私人字符串的MD5(字符串中){
消息摘要消化;
尝试 {
消化= MessageDigest.getInstance(MD5);
digest.reset();
digest.update(in.getBytes());
byte []的一个= digest.digest();
INT LEN =则为a.length;
StringBuilder的SB =新的StringBuilder(LEN<< 1);
的for(int i = 0; I< LEN;我++){
sb.append(Character.forDigit((A [1]&安培; 0XF0)GT;→4,16));
sb.append(Character.forDigit(A [1]放大器;为0x0F,16));
}
返回sb.toString();
}赶上(抛出:NoSuchAlgorithmException E){
e.printStackTrace();
}
返回null;
}
}
logcat的消息:
解决方案
可以从实体$ C消耗
内容
仅一次$ C>
在该行:
您已经消耗的内容,并再次使用的是相同的,在这里:
这就是为什么它是造成 IllegalStateException异常:内容已被消耗
因此,解决办法是在这里:
字符串_response = EntityUtils.toString(resEntity); //内容将消耗一次
最后的JSONObject jObject =新的JSONObject(_response);
Log.e(XXX,_响应);
I got struck because of IllegalStateException
in the following code. Can anybody please help me? Code:
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 message:
解决方案
You can consume Content
only at once from an Entity
in the line :
you have consumed content and again you are using the same at here:
That why it is causing IllegalStateException: Content has been consumed
So the solution is here:
String _response=EntityUtils.toString(resEntity); // content will be consume only once
final JSONObject jObject=new JSONObject(_response);
Log.e("XXX",_response);
这篇关于IllegalStateException异常:内容已被消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!