我对android编程很新,而且我已经坚持使用这段代码了好几天了。 我得到的错误是“无效使用SingleClientConnManager:连接仍然分配。” - 我理解为什么会这样,但不知道如何解决它。我正在处理的代码:
protected class loader extends AsyncTask<String, Void, String> {
protected void onPreExecute()
{
nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("username_in",username.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("password_in",password.getText().toString().trim()));
tv.setText("Started onPreExecute");
}
@Override
protected String doInBackground(String... params) {
try {
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://www.myurl.com/phplogin.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
//Toast.makeText(MainActivity.this,response, Toast.LENGTH_LONG).show();
if(response.equalsIgnoreCase("User Found"))
{
httppost.getEntity().consumeContent();
//startActivity(new Intent("com.example.obligatorisk.BROWSER"));
}
}
catch(IOException e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result)
{
tv.setText("Reached onPostExecute");
}
// TODO Auto-generated method stub
}
完整错误日志:
10-18 14:30:52.134: W/SingleClientConnManager(3940): Invalid use of SingleClientConnManager: connection still allocated.
10-18 14:30:52.134: W/SingleClientConnManager(3940): Make sure to release the connection before allocating another one.
10-18 14:30:52.414: W/System.err(3940): org.apache.http.client.HttpResponseException: Internal Server Error
10-18 14:30:52.414: W/System.err(3940): at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:71)
10-18 14:30:52.424: W/System.err(3940): at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59)
10-18 14:30:52.424: W/System.err(3940): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
10-18 14:30:52.424: W/System.err(3940): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
10-18 14:30:52.424: W/System.err(3940): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
10-18 14:30:52.424: W/System.err(3940): at com.example.obligatorisk.MainActivity$loader.doInBackground(MainActivity.java:113)
10-18 14:30:52.424: W/System.err(3940): at com.example.obligatorisk.MainActivity$loader.doInBackground(MainActivity.java:1)
10-18 14:30:52.424: W/System.err(3940): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-18 14:30:52.434: W/System.err(3940): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-18 14:30:52.434: W/System.err(3940): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-18 14:30:52.434: W/System.err(3940): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-18 14:30:52.443: W/System.err(3940): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-18 14:30:52.443: W/System.err(3940): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-18 14:30:52.443: W/System.err(3940): at java.lang.Thread.run(Thread.java:856)
该项目背后的想法是为应用程序创建登录功能。
我一直试图在网上找到解决方案和帮助,但无论我尝试什么,我都无法弄明白。我真的希望有人能抽出时间来帮助我。如果您需要有关代码,错误等的更多信息,请告诉我,我将添加它。
谢谢,感谢所有早些时候帮助过我的人。
答案 0 :(得分:0)
如果响应等于“User Found”,则只执行httppost.getEntity().consumeContent();
。无论如何,您必须完全使用内容。将该方法调用移到条件之外。
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
//Toast.makeText(MainActivity.this,response, Toast.LENGTH_LONG).show();
if(response.equalsIgnoreCase("User Found"))
{
//startActivity(new Intent("com.example.obligatorisk.BROWSER"));
}
httppost.getEntity().consumeContent();