title: Android - android.os.NetworkOnMainThreadException
body: <p>I have this exception and I was reading a thread on this, and it seemed confusing:</p>
  
  <p><a href="https://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception">How to fix android.os.NetworkOnMainThreadException?</a></p>
  
  <p>I already added this line to my manifest:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  </code></pre>
  
  <p>On that discussion, they talk about the main execution thread of the app not being able to do networking. What I am wondering is how to restructure my code so that it is inline with Android good practices.</p>
  
  <p>Here is my Activity class for this:</p>
  
  <pre><code>package com.problemio;
  
  import java.io.InputStream;
  import java.util.ArrayList;
  
  import org.apache.http.HttpEntity;
  import org.apache.http.HttpResponse;
  import org.apache.http.NameValuePair;
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.entity.UrlEncodedFormEntity;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.message.BasicNameValuePair;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  public class LoginActivity extends Activity 
  {
      public void onCreate(Bundle savedInstanceState) 
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.login);
  
          // Show form for login_email
          final EditText loginEmail = (EditText) findViewById(R.id.login_email);  
          String name = loginEmail.getText().toString();  
  
          // Show field for password  
          final EditText password = (EditText) findViewById(R.id.password);  
          String text = password.getText().toString();                  
  
          // Show button for submit
          Button submit = (Button)findViewById(R.id.submit);   
  
  
  
  
          // Show options for create-profile and forgot-password
  
  
  
  
          submit.setOnClickListener(new Button.OnClickListener() 
          {  
             public void onClick(View v) 
             {
                String email = loginEmail.getText().toString();
                String pass = password.getText().toString(); 
                sendFeedback(pass, email);
              }
          });        
      }
  
  
      public void sendFeedback(String pass , String email) 
      {  
          Log.d( "1" , pass );
          Log.d( "1" , email );
  
          // Go to db and check if these r legit
          // How do I do that? :)
          ArrayList&lt;NameValuePair&gt; postParameters = new ArrayList&lt;NameValuePair&gt;();  
          postParameters.add(new BasicNameValuePair("username", email ));  
          postParameters.add(new BasicNameValuePair("password", pass ));
  
          String responseString = null;
  
          try 
          {  
              HttpClient httpclient = new DefaultHttpClient();
              HttpPost httppost = new HttpPost("myUrl");
  
              // no idea what this does :)
              httppost.setEntity(new UrlEncodedFormEntity(postParameters));
  
              // This is the line that send the request
              HttpResponse response = httpclient.execute(httppost);
  
              HttpEntity entity = response.getEntity();            
  
              InputStream is = entity.getContent();
          } 
          catch (Exception e) 
          {     
              Log.e("log_tag", "Error in http connection "+e.toString());
          }        
      }          
  }
  </code></pre>
  
  <p>What am I doing wrong here and how could I fix it? :) Thanks!!</p>