Skip to main content

Connecting to a remote mysql database using php and Android?

Connecting to a remote database has been a Herculean Task for some programmers. No more worries here is a simple post to make it really simple. Follow these steps exactly to get it done.

Remember these when you are working with an online database.

1. Make sure that you have a running server.
2. INTERNET permission should be present in the AndroidManifest file of your app.
3. The path that you give in your program to point to the server file should be correct.

After ensuring all these you are ready to go.

First we will start with the Server side. Here I am going with a PHP example.

I am getting a value from the Android side by using a variable say "my_val".
Then the PHP script will look like this.
Note that here I am only sending one value from the Android side to the PHP Side.
so the Server side code will look like this.

$val = $_POST['my_val'];
echo $val;

Look at the varaible name inside the $_POST. Make sure that you use the same variable name in the Android side also. It is also case sensitive.

Here I am echoing back any value that is sending from the Android side.
You can do anything with it in the server side. Here I am just echoing it back.
You can simply insert it into a database and echo something else. Anything you echo will be received by the Android

Our Server side code is complete
Now we switch on to the Android side.

This is the code that connects with the server.

Thread thread = new Thread()
			 @Override public void run()
					 httpclient=new DefaultHttpClient();
					 httppost= new HttpPost("your url");
		   // make sure the url is correct.
		   //add your data
		   nameValuePairs = new ArrayList<NameValuePair>(1);
		   nameValuePairs.add(new BasicNameValuePair("my_val","my Test value from the Android Side"));
		   httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		 //Execute HTTP Post Request response=httpclient.execute(httppost);
		   ResponseHandler<String> responseHandler = new BasicResponseHandler();
		   final String response = httpclient.execute(httppost, responseHandler);
		   System.out.println("Response : " + response);
		 catch(Exception e)
			 System.out.println("Exception : " + e.getMessage());

If you are working on LocalHost use url ""

OK Done. You have successfully connected to a remote server. Check your LogCat for the value from the server Make sure you have internet connection before doing this, otherwise exception will be thrown. By this way you can send any number of variables from the Android side to the PHP side but make sure that both have the same name (case sensitive).


Popular posts from this blog

Passing Images between Activities in Android

in First Activity:
Intent intent=new Intent(FirstClass.this, SecondClass.class); Bundle bundle=new Bundle(); bundle.putInt("image",R.drawable.ic_launcher); intent.putExtras(bundle); startActivity(intent); in Second Acticity:
Bundle bundle=this.getIntent().getExtras(); int pic=bundle.getInt("image"); v.setImageResource(pic);another method:
in First Activity:
Drawable drawable=imgv.getDrawable(); Bitmap bitmap= ((BitmapDrawable)drawable).getBitmap(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] b = baos.toByteArray(); Intent intent=new Intent(Passimage.this,myclass.class); intent.putExtra("picture", b); startActivity(intent); in Second Acticity:
Bundle extras = getIntent().getExtras(); byte[] b = extras.getByteArray("picture"); Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length); Image…

Get a single record from SQLite in ANDROID

in DATABASE HELPER class: String[] coloumns = new String[] { KEY_ROWID, KEY_NAME, KEY_NICKNAME }; // calling elements in an array Cursor c = db.query(DATABASE_TABLE, coloumns, KEY_ROWID + "=" + l,null, null, null, null); if (c != null) { c.moveToFirst(); String name = c.getString(1); // since name is in position 1 ie second coloumn return name; } return null; in : String s =editinfo.getText().toString(); long l=Long.parseLong(s); DBAdapter adapter=new DBAdapter(SQliteExample.this);; String returnedname=adapter.returnName(l); String returnednickname=adapter.returnickname(l); adapter.close(); editName.setText(returnedname); editNickName.setText(returnednickname);

TextView Animation or Alpha animation (FadeIn , FadeOut) in ANDROID

An Alpha Animation is animation that controls the alpha level of an object, i.e. fading it in and out. In Android, you can apply that fading effect to almost anything, from simple text, to images, buttons, check boxes, etc…

This can be done both from xml and from java class.

Here I will explain a simple way to give a FadeIn and FadeOut for a textview on button click.. public class Textviewanimation extends Activity { Animation fadeIn,fadeOut; TextView tv; Button startanimation; Boolean fadeflag=true; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_textviewanimation); tv=(TextView) findViewById(; startanimation=(Button) findViewById(; fadeIn = new AlphaAnimation(0.0f , 1.0f ) ; fadeIn.setDuration(1200); fadeIn.setFillAfter(true); fadeOut = new AlphaAnimation(1.0f , 0.0f); fadeOut.setDurati…