Skip to main content

Connecting mysql Database in ANDROID using PHP & JSON

 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 :





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

Post a Comment

Popular posts from this blog

Why Domain-Specific AI Agents Beat One Big Agent

Everyone is building agents right now. Real estate firms. Independent insurance brokers. Fortune 500 companies with budgets big enough to hire an army of consultants. Ask around and you'll hear the same story everywhere: "we're building our own agent." And yet almost nobody is asking the obvious question: why does the default approach keep failing? One large, general-purpose agent gets wired up to every tool the business owns. It impresses in the demo. Then it quietly stalls before production. There's a gap between what businesses want and what they're actually getting. They want AI woven into their data, their workflows, their day-to-day operations. What they get instead is one oversized agent trying to be a sales rep, a compliance officer, and a customer support line, all at once. That gap is an architecture problem, not a model problem. Key Takeaways The default "one big agent" pattern breaks down on context bloat, cost, fragility, and portability...

How Create AVD for Samsung Galaxy Tab

1. Open the Android AVD and SDK Manager  2.  Select Available Packages in the left panel of AVD Manager. 3.  Click “Add ADD-on Site” and enter the URL below. http://innovator.samsungmobile.com/android/repository/srepository.xml   4. Check Samsung GALAXY Tab Add-on packages and click install button. 5. Check Samsung GALAXY Tab Add-on license & Click install button. 6) After downloading and installation of GALAXY Tab Add-on, you should restart ADB (Android Debug Bridge) or Eclipse. The important specs of this device, from an Emulator perspective, are: Target platform: Android 2.2, Galaxy Tab Screen Info: High Density, despite the fact that it’s not, this is what it reports, WSVGA (1024×600) No keyboard Has dual cameras Let’s create an AVD configuration called GalaxyTab: Within Eclipse, launch the Android SDK and AVD Manager Select “Virtual Devices” from the left-hand options Click the “New” button to create a new AVD configura...

Android show Data from Sqlite DB into Grid View

Shaadi.com Matrimonials Shaadi.com Indian Matrimonials Your Main Activity class package com . Sqlite_grid_view ; import java . util . ArrayList ; import java . util . List ; import android . app . Activity ; import android . os . Bundle ; import android . util . Log ; import android . view . View ; import android . widget . AdapterView ; import android . widget . AdapterView . OnItemClickListener ; import android . widget . ArrayAdapter ; import android . widget . GridView ; import android . widget . TextView ; import android . widget . Toast ; public class AndroidSQLiteTutorialActivity extends Activity { private GridView gridView ; public static ArrayList < String > ArrayofName = new ArrayList < String >(); /** Called when the activity is first created. */ @ Override public void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ); setContentView ( R . l...