title: HttpURLConnection in Android
body: <p>I am trying to establish a connection from my android application to a database made in MySQL. For that I am first trying to establish a connection from my app to a dummy database i created in phpMyAdmin because I don't want to corrupt my live data in my database.
  I am using a <code>HttpURLConnection</code> to post the form i created, below is my code.</p>
  
  <pre><code>public class Login1 extends Activity{
  
  
  private EditText  username=null;
      private EditText  password=null;
      //private HttpClient httpclient=null;
      //private HttpPost httppost =null;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.login_screen);
      //httpclient = new DefaultHttpClient();
      //httppost = new HttpPost("http://localhost/abc/index.php");
      username = (EditText)findViewById(R.id.editText1);
      password = (EditText)findViewById(R.id.editText2);
      Button submit = (Button)findViewById(R.id.button2);
  
      submit.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              String  uname = username.getText().toString();
              String  pwd = password.getText().toString();
              String[] strVal = new String[]{uname, pwd};
              AsyncSendData async = new AsyncSendData();
              async.execute(strVal);
          }
      });
  
  }
  
  // AsyncTask added here
  public class AsyncSendData extends AsyncTask&lt;String, Void, String&gt;{
      private static final String TAG = "Test String ";
      @Override
      protected String doInBackground(String... params  ) {
          //String limit = "|";
          //String[] strVals = values.split(limit);
          HttpClient client = new DefaultHttpClient();
          String postURL = ("http://127.0.0.1/abc/index.php");
          HttpPost post = new HttpPost(postURL);
          try {
              // Add the data
              List&lt;NameValuePair&gt; pairs = new ArrayList&lt;NameValuePair&gt;(3);
              pairs.add(new BasicNameValuePair("myusername", params[0]));
              pairs.add(new BasicNameValuePair("mypassword", params[1]));
              UrlEncodedFormEntity uefe = new UrlEncodedFormEntity(pairs);
              post.setEntity(uefe);
              // Execute the HTTP Post Request
              Log.d(TAG, "Hello!");
              try {
                  HttpResponse response = client.execute(post);
                  Log.d(TAG, ""+response);
  
              } catch (ClientProtocolException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
          catch(Exception e)
          {
  
          }
          return "test";
      }
  }
  }
  </code></pre>
  
  <p>The code for my php script is-</p>
  
  <pre><code>&lt;?php
  $host="localhost"; // Host name 
  $username="root"; // Mysql username 
  $password=""; // Mysql password 
  $db_name="test"; // Database name 
  
  // Connect to server and select databse.
  $con = mysqli_connect("$host", "$username", "$password", "$db_name")or die("cannot connect"); 
  //mysqli_select_db("$db_name")or die("cannot select DB");
  ?&gt;
  
  &lt;?php
  include("dbpath.php");
  
  $myusername=$_POST['myusername']; 
  $mypassword=$_POST['mypassword']; 
  
  // To protect MySQL injection (more detail about MySQL injection)
  //$myusername = stripslashes($myusername);
  //$mypassword = stripslashes($mypassword);
  //$myusername = mysql_real_escape_string($myusername);
  //$mypassword = mysql_real_escape_string($mypassword);
  //$sql="SELECT * FROM usertable WHERE username='$myusername' and password='$mypassword'";
  $sql = "Insert into usertable (`username`,`password`) values ('$myusername', '$mypassword')";
  $result = $con-&gt;query($sql);
  
  //while($row = mysqli_fetch_array($result)) { 
  // echo "Success";
  //} 
  echo "success";
  ?&gt;
  </code></pre>
  
  <p>Now, when I run the debugger to check for bugs it gets stuck where i define</p>
  
  <pre><code>HttpResponse response = client.execute(post);
  </code></pre>
  
  <p>Throws <code>IOException</code> on this line</p>
  
  <p>My LogCat</p>
  
  <pre><code>    07-31 07:53:21.580: I/Choreographer(1528): Skipped 41 frames!  The application may be doing too much work on its main thread.
      07-31 07:53:22.400: I/Choreographer(1528): Skipped 30 frames!  The application may be doing too much work on its main thread.
      07-31 07:53:23.021: I/Choreographer(1528): Skipped 36 frames!  The application may be doing too much work on its main thread.
      07-31 07:53:44.420: D/Test String(1528): Hello!
      07-31 07:53:44.800: W/System.err(1528):     org.apache.http.conn.HttpHostConnectException: Connection to http://127.0.0.1 refused
      07-31 07:53:44.810: W/System.err(1528):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
      07-31 07:53:44.820: W/System.err(1528):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
      07-31 07:53:44.840: W/System.err(1528):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
      07-31 07:53:44.860: W/System.err(1528):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
      07-31 07:53:44.880: W/System.err(1528):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
      07-31 07:53:44.960: W/System.err(1528):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
      07-31 07:53:44.970: W/System.err(1528):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
      07-31 07:53:44.990: W/System.err(1528):     at com.myProject.Project1.Login1$AsyncSendData.doInBackground(Login1.java:124)
      07-31 07:53:45.010: W/System.err(1528):     at com.myProject.Project1.Login1$AsyncSendData.doInBackground(Login1.java:1)
      07-31 07:53:45.020: W/System.err(1528):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
      07-31 07:53:45.030: W/System.err(1528):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
      07-31 07:53:45.040: W/System.err(1528):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
      07-31 07:53:45.060: W/System.err(1528):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
      07-31 07:53:45.080: W/System.err(1528):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
      07-31 07:53:45.090: W/System.err(1528):     at java.lang.Thread.run(Thread.java:856)
      07-31 07:53:45.100: W/System.err(1528): Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)
      07-31 07:53:45.120: W/System.err(1528):     at libcore.io.IoBridge.connect(IoBridge.java:114)
      07-31 07:53:45.130: W/System.err(1528):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
      07-31 07:53:45.140: W/System.err(1528):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
      07-31 07:53:45.150: W/System.err(1528):     at java.net.Socket.connect(Socket.java:842)
      07-31 07:53:45.181: W/System.err(1528):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
      07-31 07:53:45.201: W/System.err(1528):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
      07-31 07:53:45.220: W/System.err(1528):     ... 14 more
      07-31 07:53:45.240: W/System.err(1528): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
      07-31 07:53:45.290: W/System.err(1528):     at libcore.io.Posix.connect(Native Method)
      07-31 07:53:45.310: W/System.err(1528):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
      07-31 07:53:45.321: W/System.err(1528):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
      07-31 07:53:45.330: W/System.err(1528):     at libcore.io.IoBridge.connect(IoBridge.java:112)
      07-31 07:53:45.340: W/System.err(1528):     ... 19 more
      07-31 07:54:26.030: E/ThrottleService(273): problem during onPollAlarm: java.lang.IllegalStateException: problem parsing stats: java.io.FileNotFoundException: /proc/net/xt_qtaguid/iface_stat_all: open failed: ENOENT (No such file or directory)
  </code></pre>