Connecting mysql Database in ANDROID using PHP & JSON

14:25:00 Dhaval Sodha Parmar 16 Comments

 To implement this tutorial you should have basic knowledge of how to run PHP script and start server.  If we talk about client-server architecture, client is Android device and in server side there is a combination of PHP Script and MySQL. In short, PHP Script sits in middle as shown in image.



Lets suppose that we have a MySQL database named Employee, and a table int created, with the following SQL:



CREATE TABLE `employee` (
  `emp_id` int(11) NOT NULL auto_increment,
  `emp_name` varchar(100) NOT NULL,
  PRIMARY KEY  (`emp_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;


The PHP code will be very simple:



Now Create Android Project : 


The Android part is only a bit more complicated:
-use a HttpPost to get the data

-convert response to string
-parse JSON data in to List



In Your First Activity :



O/P :





16 comments:

  1. Hi
    I tried ur coding.I have got an error that the variables "items" and "is" can not be resolved.Where it should be declared.

    ReplyDelete
    Replies
    1. Both items & is are Global Variable. like:

      public class mainActivity extends Activity{

      public ArrayList items = new arrayList();
      String is;

      @Override
      public void onCreate(Bundle savedInstanceState)
      // rest of all code
      }

      }

      Delete
  2. Thank you.But when the app starts i encounter this error
    05-21 17:11:58.115: E/log_tag(410): Error in HTTP connectionjava.net.SocketException: Permission denied
    05-21 17:11:58.146: E/log_tag(410): Error in convert Stringjava.lang.NullPointerException

    ReplyDelete
    Replies
    1. You have added internet permission in android manifest file??

      http://stackoverflow.com/questions/2378607/what-permission-do-i-need-to-access-internet-from-an-android-application

      Delete
  3. Can you please tell the declaration for sb and result? Thanks!

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. I've managed to make the program work. However, I seem to encounter an unexpected error. What is shown on the browser when I run the php file separately also shows when I run the application on my Android phone. Any thoughts on what may be the cause? Thanks!

    ReplyDelete
    Replies
    1. Could you show me what type of error you get? Mean show me error logs.

      Hint: run your php file in browser (chrome or any other) in local server (http://192.168.0.3/projects/test.php) online (http://xyz.com/projects/test.php) check there is any error if not then add INTERNET & Access network state permission in your android project.

      Do above things...

      Delete
    2. Sorry. Error may not have been the best choice of word. There is actually no error message given in logcat. When I run the hp file in the browser using localhost, it shows

      {"emp_id":"1234567890","emp_name":"Sample Name"}

      When I run the program in my Android phone, and message shows

      "no data found {"emp_id":"1234567890","emp_name":"Sample Name"}" .

      Through experimentation, I managed to remove the message from appearing when I run the program on my phone by deleting the

      Toast.makeText(this, "no data found" + result, Toast.LENGTH_LONG).show();

      line on the last try{} group in your code. My new question is, would removing that line have a significant effect on the program as a whole? Thanks again for helping me on this.

      Delete
    3. if you are get this ---> {"emp_id":"1234567890","emp_name":"Sample Name"} mean you got the result. now time to do json parsing.

      Delete
  6. Where can I download the code from?
    Thank you..

    ReplyDelete
    Replies
    1. sorry you have to watch & develop code... i have't upload code of this example

      Delete
    2. Can you extend this example by including also clicking on the items of the Listview and taking the id in each row of the list to display them in a 2nd activity?
      It would be really interesting.. :-)

      Delete
  7. on defining items as String it is giving error on items.add("List") :
    The method add(String) is undefined for the type String.

    ReplyDelete