我在使用 AsyncTask 时遇到了一些问题。如果用户名正确,我试图访问一个应该连接并检查 MySQL 数据库的 php 文件。
我得到的错误如下:
12-01 14:51:25.815: W/dalvikvm(3216): threadid=11: thread exiting with uncaught exception (group=0x40a13300)
12-01 14:51:25.927: E/AndroidRuntime(3216): FATAL EXCEPTION: AsyncTask #1
12-01 14:51:25.927: E/AndroidRuntime(3216): java.lang.RuntimeException: An error occured while executing doInBackground()
12-01 14:51:25.927: E/AndroidRuntime(3216): at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-01 14:51:25.927: E/AndroidRuntime(3216): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-01 14:51:25.927: E/AndroidRuntime(3216): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-01 14:51:25.927: E/AndroidRuntime(3216): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-01 14:51:25.927: E/AndroidRuntime(3216): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-01 14:51:25.927: E/AndroidRuntime(3216): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-01 14:51:25.927: E/AndroidRuntime(3216): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-01 14:51:25.927: E/AndroidRuntime(3216): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-01 14:51:25.927: E/AndroidRuntime(3216): at java.lang.Thread.run(Thread.java:856)
12-01 14:51:25.927: E/AndroidRuntime(3216): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
12-01 14:51:25.927: E/AndroidRuntime(3216): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4607)
12-01 14:51:25.927: E/AndroidRuntime(3216): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:835)
12-01 14:51:25.927: E/AndroidRuntime(3216): at android.view.View.requestLayout(View.java:15129)
12-01 14:51:25.927: E/AndroidRuntime(3216): at android.view.View.requestLayout(View.java:15129)
12-01 14:51:25.927: E/AndroidRuntime(3216): at android.view.View.requestLayout(View.java:15129)
12-01 14:51:25.927: E/AndroidRuntime(3216): at android.view.View.requestLayout(View.java:15129)
12-01 14:51:25.927: E/AndroidRuntime(3216): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:292)
12-01 14:51:25.927: E/AndroidRuntime(3216): at android.view.View.requestLayout(View.java:15129)
12-01 14:51:25.927: E/AndroidRuntime(3216): at android.view.View.requestLayout(View.java:15129)
12-01 14:51:25.927: E/AndroidRuntime(3216): at android.widget.TextView.checkForRelayout(TextView.java:6309)
12-01 14:51:25.927: E/AndroidRuntime(3216): at android.widget.TextView.setText(TextView.java:3547)
12-01 14:51:25.927: E/AndroidRuntime(3216): at android.widget.TextView.setText(TextView.java:3405)
12-01 14:51:25.927: E/AndroidRuntime(3216): at android.widget.TextView.setText(TextView.java:3380)
12-01 14:51:25.927: E/AndroidRuntime(3216): at com.example.obligatorisk.MainActivity$loader.doInBackground(MainActivity.java:101)
12-01 14:51:25.927: E/AndroidRuntime(3216): at com.example.obligatorisk.MainActivity$loader.doInBackground(MainActivity.java:1)
12-01 14:51:25.927: E/AndroidRuntime(3216): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-01 14:51:25.927: E/AndroidRuntime(3216): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-01 14:51:25.927: E/AndroidRuntime(3216): ... 5 more
(对不起,文字墙,不确定要包括什么)
我正在使用的代码:
protected class loader extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
try {
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://myurl.com/phplogin.php");
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()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httppost.getEntity().consumeContent();
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
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"));
}
}
catch(IOException e){
e.printStackTrace();
}
return null;
}
// TODO Auto-generated method stub
}
有人可以帮我吗?我被困了好几天了。