我的 android 应用程序中的代码存在问题。我以前没有处理过异步,我不确定如何调试这个问题。出于安全原因,URL 已被替换。
class checkLogin extends AsyncTask<String, Void, Boolean> {
protected Boolean doInBackground(String... params) {
String urlStr = "http://example.com/api/?username=" + params[0] + "&password=" + params[1] + "&action=checkLogin";
try{
URL url = new URL(urlStr);
HttpURLConnection connection= (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.connect();
BufferedReader reader= new BufferedReader(new InputStreamReader(connection.getInputStream()));
String output = "";
String str;
while ((str= reader.readLine()) != null) {
output += "\r\n" + str;
}
createMsg(output,"Debug");
return true;
}catch(Exception e){
createMsg("Encountered an error. Report this problem to the development department with steps to reproduce","Fatal Error");
return false;
}
}
}
提前致谢!
编辑: createMsg 创建一个 android 警报框。
错误:
06-07 18:19:35.066: E/AndroidRuntime(1206): FATAL EXCEPTION: AsyncTask #1
06-07 18:19:35.066: E/AndroidRuntime(1206): java.lang.RuntimeException: An error occured while executing doInBackground()
06-07 18:19:35.066: E/AndroidRuntime(1206): at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-07 18:19:35.066: E/AndroidRuntime(1206): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
06-07 18:19:35.066: E/AndroidRuntime(1206): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
06-07 18:19:35.066: E/AndroidRuntime(1206): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
06-07 18:19:35.066: E/AndroidRuntime(1206): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-07 18:19:35.066: E/AndroidRuntime(1206): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-07 18:19:35.066: E/AndroidRuntime(1206): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-07 18:19:35.066: E/AndroidRuntime(1206): at java.lang.Thread.run(Thread.java:856)
06-07 18:19:35.066: E/AndroidRuntime(1206): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
06-07 18:19:35.066: E/AndroidRuntime(1206): at android.os.Handler.<init>(Handler.java:197)
06-07 18:19:35.066: E/AndroidRuntime(1206): at android.os.Handler.<init>(Handler.java:111)
06-07 18:19:35.066: E/AndroidRuntime(1206): at android.app.Dialog.<init>(Dialog.java:107)
06-07 18:19:35.066: E/AndroidRuntime(1206): at android.app.AlertDialog.<init>(AlertDialog.java:114)
06-07 18:19:35.066: E/AndroidRuntime(1206): at android.app.AlertDialog$Builder.create(AlertDialog.java:931)
06-07 18:19:35.066: E/AndroidRuntime(1206): at com.unrealonline.timesheet.MainActivity.createMsg(MainActivity.java:55)
06-07 18:19:35.066: E/AndroidRuntime(1206): at com.unrealonline.timesheet.MainActivity$checkLogin.doInBackground(MainActivity.java:79)
06-07 18:19:35.066: E/AndroidRuntime(1206): at com.unrealonline.timesheet.MainActivity$checkLogin.doInBackground(MainActivity.java:1)
06-07 18:19:35.066: E/AndroidRuntime(1206): at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-07 18:19:35.066: E/AndroidRuntime(1206): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-07 18:19:35.066: E/AndroidRuntime(1206): ... 4 more