Can connect android login app with a remote database hosted on a ubuntu machine
<p>I am building an small android app to connect to databae and check if the username and password matches then it should give me message and else || . I have my database hosted on my virtual box Ubuntu.</p>

<pre><code>&lt;?php
    $con= mysql_connect("localhost","root","123456") or die ("can't connect");

    mysql_select_db("androiddb",$con) or die("can't select the database");
    $username = $_POST['username'];
    $password = $_POST['password'];
    $query = mysql_query("SELECT * FROM androidlogin WHERE user=$username' AND                        pass=$password");

    $num = mysql_num_rows($query);
    if($num == 1){

        while($list = mysql_fetch_assoc($num)){

            $output[] = $list;

            echo json_decode($output);
        }
    }
    mysql_close();
</code></pre>

<p>my java file. 
        package com.example.logintutorial;</p>

<pre><code>    /// solved the problem from this link
    //http://stackoverflow.com/questions/3538647/android-development-button-onclicklisteners-help
    //import android.view.View.OnClickListener;

    public class MainActivity extends Activity implements OnClickListener, android.view.View.OnClickListener {

        EditText etUser,etPass;
        Button bLogin;
        String username, password;
        // use http clinet as form of container
        HttpClient httpClient;

        HttpPost httpposts;
         //create an arry list of data to be sent
        ArrayList&lt;NameValuePair&gt; nameValuePairs;
        // http response http entity
        HttpResponse response;
        HttpEntity entity;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            initialise();
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }

        public void initialise(){
            etUser = (EditText) findViewById(R.id.etUser);
            etPass = (EditText) findViewById(R.id.etPass);
            bLogin = (Button) findViewById(R.id.etSubmit);
            bLogin.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            //defualt http clinet

            DefaultHttpClient httpclient = new DefaultHttpClient();

            final String KEY_121= "http://192.168.1.139:8080/android_login/newfile.php";

            //create new HTTp post with Url to php file as master
            HttpPost httppost = new HttpPost(KEY_121);

            //ssign inpyt to the strings
            username= etUser.getText().toString();
            password= etPass.getText().toString();

            try{
                        //create a new array list
                        nameValuePairs = new ArrayList&lt;NameValuePair&gt;();

                        //place them in array list
                        nameValuePairs.add(new BasicNameValuePair("username",username));
                        nameValuePairs.add(new BasicNameValuePair("password",password));

                         // add the array to the post
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                        //assign excuted form container to response
                        response = httpclient.execute(httppost);

                        //check ing the status code
                        if(response.getStatusLine().getStatusCode()==200){
                            // assign respone to http entity
                            entity = response.getEntity();
                            //if enitty is not null
                            if(entity!=null){
                                // create an entity with reviecd data assing

                                InputStream instream = entity.getContent();
                                //Create new Json Object. assing converted data as a paramter

                                JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));

                                String retUser= jsonResponse.getString("user");//mysql field
                                String retPass= jsonResponse.getString("pass");

                                if(username.equals(retUser) &amp;&amp; password.equals(retPass)){

                                    //create a new shared perefere by getting the preference

                                    SharedPreferences sp =  getSharedPreferences("Logindetails",0);
                                    SharedPreferences.Editor spedit =  sp.edit();
                                    spedit.putString("user", username);
                                    spedit.putString("pass", password);

                                    spedit.commit();


                                    //login success message
                                    Toast.makeText(getBaseContext(),"You are logged in", Toast.LENGTH_SHORT).show();


                                } else{

                                    //display 
                                    Toast.makeText(getBaseContext(),"Invalid Login", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                        Toast.makeText(getBaseContext(),"Connection Error", Toast.LENGTH_SHORT).show();
                    }
        }

        @Override
        public void onClick(DialogInterface dialog, int which) {

        }

        private static String convertStreamToString(InputStream is) {

            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();

            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return sb.toString();
        }
    }
</code></pre>

<p>my manifest</p>

<pre><code>        &lt;uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" /&gt;
        &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
        &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
        &lt;application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" &gt;
        &lt;activity
            android:name="com.example.logintutorial.MainActivity"
            android:label="@string/app_name" &gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;

                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;

&lt;/manifest&gt;
</code></pre>

<p>my errors. When Click Login. I get "Connection Error" which is in the catch section. And this in my chatlog</p>

<pre><code>03-21 10:03:45.479: W/IInputConnectionWrapper(635): showStatusIcon on                      inactive InputConnection
                                       03-21 10:03:57.849: D/InputEventConsistencyVerifier(635): KeyEvent:                        ACTION_UP but key was not down.
                                       03-21 10:03:57.849: D/InputEventConsistencyVerifier(635):   in android.widget.EditText@41213f98
                                       03-21 10:03:57.849: D/InputEventConsistencyVerifier(635):   0: sent at                   182837000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0,               flags=0x8, repeatCount=0, eventTime=182837, downTime=182714, deviceId=0, source=0x101 }
                                      03-21 10:04:00.579: W/System.err(635): android.os.NetworkOnMainThreadException
                    03-21 10:04:00.589: W/System.err(635):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
                    03-21 10:04:00.589: W/System.err(635):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
                    03-21 10:04:00.589: W/System.err(635):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
                    03-21 10:04:00.589: W/System.err(635):  at libcore.io.IoBridge.connect(IoBridge.java:112)
                    03-21 10:04:00.589: W/System.err(635):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
                    03-21 10:04:00.589: W/System.err(635):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
                    03-21 10:04:00.599: W/System.err(635):  at java.net.Socket.connect(Socket.java:842)
                    03-21 10:04:00.599: W/System.err(635):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
                    03-21 10:04:00.599: W/System.err(635):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
                    03-21 10:04:00.599: W/System.err(635):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
                    03-21 10:04:00.599: W/System.err(635):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
                    03-21 10:04:00.609: W/System.err(635):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
                    03-21 10:04:00.609: W/System.err(635):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
                    03-21 10:04:00.609: W/System.err(635):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
                    03-21 10:04:00.609: W/System.err(635):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
                    03-21 10:04:00.609: W/System.err(635):  at com.example.logintutorial.MainActivity.onClick(MainActivity.java:114)
                    03-21 10:04:00.609: W/System.err(635):  at android.view.View.performClick(View.java:4084)
                    03-21 10:04:00.620: W/System.err(635):  at android.view.View$PerformClick.run(View.java:16966)
                    03-21 10:04:00.620: W/System.err(635):  at android.os.Handler.handleCallback(Handler.java:615)
                    03-21 10:04:00.620: W/System.err(635):  at android.os.Handler.dispatchMessage(Handler.java:92)
                    03-21 10:04:00.629: W/System.err(635):  at android.os.Looper.loop(Looper.java:137)
                    03-21 10:04:00.629: W/System.err(635):  at android.app.ActivityThread.main(ActivityThread.java:4745)
                    03-21 10:04:00.629: W/System.err(635):  at java.lang.reflect.Method.invokeNative(Native Method)
                    03-21 10:04:00.629: W/System.err(635):  at java.lang.reflect.Method.invoke(Method.java:511)
                    03-21 10:04:00.629: W/System.err(635):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                    03-21 10:04:00.639: W/System.err(635):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                    03-21 10:04:00.639: W/System.err(635):  at dalvik.system.NativeStart.main(Native Method)
                    03-21 10:04:05.069: W/System.err(635): android.os.NetworkOnMainThreadException
                    03-21 10:04:05.069: W/System.err(635):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
                    03-21 10:04:05.069: W/System.err(635):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
                    03-21 10:04:05.079: W/System.err(635):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
                    03-21 10:04:05.079: W/System.err(635):  at libcore.io.IoBridge.connect(IoBridge.java:112)
                    03-21 10:04:05.079: W/System.err(635):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
                    03-21 10:04:05.079: W/System.err(635):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
                    03-21 10:04:05.089: W/System.err(635):  at java.net.Socket.connect(Socket.java:842)
                    03-21 10:04:05.089: W/System.err(635):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
                    03-21 10:04:05.099: W/System.err(635):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
                    03-21 10:04:05.099: W/System.err(635):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
                    03-21 10:04:05.099: W/System.err(635):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
                    03-21 10:04:05.099: W/System.err(635):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
                    03-21 10:04:05.099: W/System.err(635):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
                    03-21 10:04:05.099: W/System.err(635):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
                    03-21 10:04:05.109: W/System.err(635):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
                    03-21 10:04:05.109: W/System.err(635):  at com.example.logintutorial.MainActivity.onClick(MainActivity.java:114)
                    03-21 10:04:05.109: W/System.err(635):  at android.view.View.performClick(View.java:4084)
                    03-21 10:04:05.119: W/System.err(635):  at android.view.View$PerformClick.run(View.java:16966)
                    03-21 10:04:05.119: W/System.err(635):  at android.os.Handler.handleCallback(Handler.java:615)
                    03-21 10:04:05.119: W/System.err(635):  at android.os.Handler.dispatchMessage(Handler.java:92)
                    03-21 10:04:05.119: W/System.err(635):  at android.os.Looper.loop(Looper.java:137)
                    03-21 10:04:05.129: W/System.err(635):  at android.app.ActivityThread.main(ActivityThread.java:4745)
                    03-21 10:04:05.129: W/System.err(635):  at java.lang.reflect.Method.invokeNative(Native Method)
                    03-21 10:04:05.129: W/System.err(635):  at java.lang.reflect.Method.invoke(Method.java:511)
                    03-21 10:04:05.129: W/System.err(635):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                    03-21 10:04:05.129: W/System.err(635):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                    03-21 10:04:05.129: W/System.err(635):  at dalvik.system.NativeStart.main(Native Method)
</code></pre>
