Why am I getting android.os.NetworkOnMainThreadException using AsyncTask?

I get android.os.NetworkOnMainThreadException while I wrote the network operation code in AsynkTask. is there any other reason to exclude this exception?

Here is my code:

public class Background_confirmation extends AsyncTask<Void, Integer, Void> { @Override protected void onPreExecute() { // TODO Auto-generated method stub progressDialog = ProgressDialog.show(Confirmation.this, "Please wait...", "Retrieving data ...", true); try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost( "http://68.121.167.160/sip_chat_api/create_account.php?useralias=" + useralias + "&cntname=" + cntcode + ""); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); } catch (Exception e) { e.printStackTrace(); } if (backgroung_flag == 1) { } else { if (is != null) { try { BufferedReader reader = new BufferedReader( new InputStreamReader(is, "UTF-8")); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result = sb.toString(); } catch (Exception e) { Log.e("log_tag", "Error converting result " + e.toString()); } } } super.onPreExecute(); } @Override protected Void doInBackground(Void... params) { // TODO Auto-generated method stub return null; } @Override protected void onPostExecute(Void result) { // TODO Auto-generated method stub if (progressDialog.isShowing()) { progressDialog.dismiss(); // progressDialog.setCancelable(true); } super.onPostExecute(result); } } 

And I call this class in OnCreate ()

 new Background_confirmation().execute(); 

But it always goes in the Catch block and gives me this LogCat exceptions
Any suggestions or ideas would be appreciated.
Thanks

+6
source share
3 answers
 public class Background_confirmation extends AsyncTask<Void, Integer, String> { @Override protected void onPreExecute() { super.onPreExecute(); progressDialog = ProgressDialog.show(Confirmation.this, "Please wait...", "Retrieving data ...", true); } @Override protected String doInBackground(Void... params) { try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://68.121.167.160/sip_chat_api/create_account.php?useralias=" + useralias + "&cntname=" + cntcode + ""); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); } catch (Exception e) { e.printStackTrace(); } if (backgroung_flag == 1) { } else { if (is != null) { try { BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result = sb.toString(); } catch (Exception e) { Log.e("log_tag", "Error converting result " + e.toString()); } } } return result; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); if (progressDialog.isShowing()) { progressDialog.dismiss(); // progressDialog.setCancelable(true); } } } 

Your code should change as above. Things You Should Consider

  • Connectivity must be encoded inside doInBackground()
  • If you want to get the result of doInBackground() , you must accept it in onPostExecute()
  • This means that you must return the String value in doInBackground() , where your third parameter of the AsyncTask class AsyncTask also be String (which is not in Wayne's answer)

In your code, you call an InputStream , which we do not see, except for the "else" part. If you use only InputStream , make sure the code always reaches the else part.

+4
source

You used the wrong AsyncTask method to put your network-related code. Please move it to doInBackground because onPreExecute happens in the main thread. So an exception occurred. Details here .

+4
source

Put all the network request code in doInBackground . onPreExecute and onPostExecute will run in the user interface thread (main thead), so you will get access if you request a network using these two methods.

 public class Background_confirmation extends AsyncTask<Void, Integer, Void> { @Override protected void onPreExecute() { progressDialog = ProgressDialog.show(Confirmation.this, "Please wait...", "Retrieving data ...", true); } @Override protected Void doInBackground(Void... params) { // TODO Auto-generated method stub try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost( "http://68.121.167.160/sip_chat_api/create_account.php?useralias=" + useralias + "&cntname=" + cntcode + ""); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); } catch (Exception e) { e.printStackTrace(); } if (backgroung_flag == 1) { } else { if (is != null) { try { BufferedReader reader = new BufferedReader( new InputStreamReader(is, "UTF-8")); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result = sb.toString(); } catch (Exception e) { Log.e("log_tag", "Error converting result " + e.toString()); } } } } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub if (progressDialog.isShowing()) { progressDialog.dismiss(); // progressDialog.setCancelable(true); } } } 
+1
source

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


All Articles