title: android.os.NetworkOnMainThreadException Trying to access database from android activity. Android Studio
body: <p>I am getting android.os.NetworkOnMainThreadException in my Login Activity. Basically I am simply trying to check if the user id and password entered by the user are correct i.e., is the user allowed to sign in or not. I have searched for a solution and know that the exception has got something to do with networking on main thread. But I am using AsyncTask. I am still getting this exception. Here is my LoginActivity.java</p>
  
  <pre><code>    public class LoginActivity extends AppCompatActivity {
  
      JSONParser jsonParser = new JSONParser();
      // single product url
      private static final String url_user_check_login = "##########################/android_connect/db_connect.php";
       int success=0;
      String id="";
      String password="";
      private static final String TAG_SUCCESS = "success";
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_login);
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  
  //        Defining onClickListener for Login Button
          Button loginBtn=(Button) findViewById(R.id.login_btn);
          loginBtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
  //                Check credentials
                  EditText phone=(EditText)findViewById(R.id.phone_txt);
                  EditText pwd=(EditText)findViewById(R.id.password_txt);
                  id=phone.getText().toString();
                  password=pwd.getText().toString();
                  new CheckUserLogin().execute();
                  if (success == 1) {
  
                      Intent intent=new Intent(getApplicationContext(), RideDetailsActivity.class);
                      startActivity(intent);
  
                  }else{
                      // product with id not found
                  }
              }
          });
      }
      class CheckUserLogin extends AsyncTask&lt;String, String, String&gt; {
  
          protected String doInBackground(String... params) {
  
              // updating UI from Background Thread
              runOnUiThread(new Runnable() {
                  public void run() {
                      // Check for success tag
  
                      try {
                          // Building Parameters
                          List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
                          params.add(new BasicNameValuePair("id", id));
  
                          // getting product details by making HTTP request
                          // Note that product details url will use GET request
                          JSONObject json = jsonParser.makeHttpRequest(
                                  url_user_check_login, "GET", params);
  
                          // check your log for json response
  //                        Log.d("Single Product Details", json.toString());
  
                          // json success tag
                          success = json.getInt(TAG_SUCCESS);
  
                      } catch (JSONException e) {
                          e.printStackTrace();
                      }
                  }
              });
  
              return null;
          }
  
  
      }
  }
  </code></pre>
  
  <p>I don't know if this is important but I tried debugging and put break points inside the <code>doInBackground</code> method but my debugger just ignores the break points and it just blows past the new <code>CheckUserLogin().execute();</code> statement.</p>
  
  <p>Also here is my exception stack trace.</p>
  
  <pre><code>    FATAL EXCEPTION: main
  android.os.NetworkOnMainThreadException
  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)
  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
  at java.net.InetAddress.getAllByName(InetAddress.java:214)
  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:587)
  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:511)
  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:489)
  at com.heycabs.heycabs.JSONParser.makeHttpRequest(JSONParser.java:65)
  at com.heycabs.heycabs.LoginActivity$CheckUserLogin$1.run(LoginActivity.java:98)at android.os.Handler.handleCallback(Handler.java:800)
  at android.os.Handler.dispatchMessage(Handler.java:100)
  at android.os.Looper.loop(Looper.java:194)
  at android.app.ActivityThread.main(ActivityThread.java:5409)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:525)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>