Error executing doInBackground () android

I have a problem with my code in an Android app. I have not dealt with Async before, and I'm not sure how to debug the problem. For security reasons, the URL has been replaced.

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; } } } 

Thanks in advance!

EDIT: createMsg creates an Android alert window.

Error:

 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 
+4
source share
2 answers

you can use runOnUiThread or Hanlder to display Toast or Alertbox from a non-interface (in your case, from doInBackground ). do it like:

 try{ //.....your code here... while ((str= reader.readLine()) != null) { output += "\r\n" + str; } Your_Current_Activity.this.runOnUiThread(new Runnable() { public void run() { createMsg(output,"Debug"); // show message here } }); return true; }catch(Exception e){ //use runOnUiThread for showing Alert here.... return false; } 
+11
source

The doInBackground method in AsyncTask should not perform any actions on the display (user interface), since it is not specifically included in the user interface stream. Although you can achieve this using the approach published in @ ρσѕρє K's answer, AsyncTask's point was to provide a simple mechanism for accessing the user interface from the background stream in a structured way.

When you call createMsg from doInBackground , you break this paradigm. All user interface actions in AsyncTask should, in principle, begin with:

  • onPreExecute to be executed before doInBackground
  • onPostExecute , which will be launched after the completion of doInBackground
  • onProgressUpdate , which will run whenever publishProgress is called from doInBackground

The Android dev page contains a good use case.

In your case, it looks like you should return a success or failure code from doInBackground , which is picked up by onPostExecute ; it should call createMsg if it received a failure code.

+3
source

Source: https://habr.com/ru/post/1485096/


All Articles