error android.os.networkonmainthreadexception asynctask getResponseCode

android

Solution

use asynctask.execute for executing AsyncTask instead of calling doInBackground manually as:

 TestConnectionNew t = new TestConnectionNew();
 t.execute("http://longvansolution.tk/monthlytarget.php");

Change your TestConnectionNew as

public class TestConnectionNew extends AsyncTask<String, Void, String> {
private int responseHttp = 0;
private String flag="false";

@Override
protected String doInBackground(String... urltest) {
    // TODO Auto-generated method stub
    try {
        URL url = new URL(urltest[0]);
        URLConnection connection = url.openConnection();
        connection.setConnectTimeout(2000);
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        **responseHttp = httpConnection.getResponseCode();**
        if (responseHttp == HttpURLConnection.HTTP_OK) {
            flag = "true";
        } else {
            flag = "false";
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Thong bao loi: "+e.toString());
    }
    return flag;
}

@Override
     protected void onPostExecute(String recieve) {
           if(recieve.equalsIgnoreCase("true"))
            {
               doTimerTask();
           }else
           if(recieve.equalsIgnoreCase("false"))
             {
              showAlert("Không kết nối được đến server hoặc thiết bị chưa có kết nối internet!");
             }
      }
}

and for more help about how we use AsyncTask see:

http://developer.android.com/reference/android/os/AsyncTask.html

Problem

when run my code on the same class is app run good. but when i run this code on two class different, my app was error android.os.networkonmainthreadexception. when debug i detect error at responseHttp = httpConnection.getResponseCode(); app run to line responseHttp = httpConnection.getResponseCode(); and it go to catch{},it cancel "If..else".And log error android.os.networkonmainthreadexception. Can you help me!! My code class Asynctask ``` package com.example.finishdemo; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import android.os.AsyncTask; public class TestConnectionNew extends AsyncTask<String, Void, String> { private int responseHttp = 0; private String flag="false"; @Override protected String doInBackground(String... urltest) { // TODO Auto-generated method stub try { URL url = new URL(urltest[0]); URLConnection connection = url.openConnection(); connection.setConnectTimeout(2000); HttpURLConnection httpConnection = (HttpURLConnection) connection; **responseHttp = httpConnection.getResponseCode();** if (responseHttp == HttpURLConnection.HTTP_OK) { flag = "true"; } else { flag = "false"; } } catch (Exception e) { e.printStackTrace(); System.out.println("Thong bao loi: "+e.toString()); } return flag; } } ``` Code class main: ``` package com.example.finishdemo; public class Hoadon extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.hoadon); TestConnectionNew t=new TestConnectionNew(); String recieve=t.doInBackground("http://longvansolution.tk/monthlytarget.php"); if(recieve.equalsIgnoreCase("true")) { doTimerTask(); }else if(recieve.equalsIgnoreCase("false")) { showAlert("Không kết nối được đến server hoặc thiết bị chưa có kết nối internet!"); } ```

Original source