Stack Exchange
Error in mysql part
I am new to android development. I have a website already setup which uses a mysql             database. I want to know how can I connect my app to that database. What I have tried shows  an invalid Ip address error in the php part.
here is my code.
Main Activity.java
package com.example.testactivity;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;enter code here
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 org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    String id;
    String name;
    InputStream is=null;
    String result=null;
    String line=null;

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

        final EditText e_id=(EditText) findViewById(R.id.editText1);
        Button select=(Button) findViewById(R.id.button1);
        select.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            id=e_id.getText().toString();
            select();
        }
    });
    }

    public void select()
    {
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

    nameValuePairs.add(new BasicNameValuePair("id",id));

        try
        {
        HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://192.168.1.5/select.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost); 
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            Log.e("pass 1", "connection success ");
    }
        catch(Exception e)
    {
            Log.e("Fail 1", e.toString());
            Toast.makeText(getApplicationContext(), "Invalid IP Address",
            Toast.LENGTH_LONG).show();
    }     

        try
        {
            BufferedReader reader = new BufferedReader
                (new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null)
        {
                sb.append(line + "\n");
            }
                is.close();
                result = sb.toString();
            Log.e("pass 2", "connection success ");
    }
        catch(Exception e)
        {
        Log.e("Fail 2", e.toString());
    }     

    try
        {
            JSONObject json_data = new JSONObject(result);
            name=(json_data.getString("name"));
        Toast.makeText(getBaseContext(), "Name : "+name,
            Toast.LENGTH_SHORT).show();
        }
        catch(Exception e)
        {
            Log.e("Fail 3", e.toString());
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }    
}

activity_main.Xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="39dp"
        android:padding="11dp"
        android:hint="Id"
        android:ems="10"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:padding="11dp"
        android:text="Select" />

</RelativeLayout>

Select.php
<?php
    $host='localhost';
    $uname='root';
    $pwd='';
    $db="android";

    $con = mysql_connect($host,$uname,$pwd) or die("connection failed");
    mysql_select_db($db,$con) or die("db selection failed");

    $id=$_REQUEST['id'];

    $r=mysql_query("select * from category where id='$id'",$con);

    while($row=mysql_fetch_array($r))
    {
        $flag[name]=$row[name];
    }

    print(json_encode($flag));
    mysql_close($con);
?>


Please post the log cat.

09-06 10:54:30.080: I/Choreographer(620): Skipped 32 frames!  The application may be doing too much work on its main thread.
09-06 10:54:30.100: D/gralloc_goldfish(620): Emulator without GPU emulation detected.
09-06 10:54:36.449: E/Fail 1(620): android.os.NetworkOnMainThreadException
09-06 10:54:36.479: E/Fail 2(620): java.lang.NullPointerException
09-06 10:54:36.479: E/Fail 3(620): java.lang.NullPointerException

Do you have android.permission.INTERNET permission in the manifest file?
