Category Archives: PHP-MYSQL Connections

How to find your Google Plus ID

This is so simple

1. Go to your Google + account (https://plus.google.com/).

2. Click on the Profile icon on the Left.

3. If you look at the URL in the address bar, it should look something like this:

https://plus.google.com/104653270154306099169/posts

4. The long numerical string in the URL is your Google+ ID. Here is CoderzHeaven’s from the URL above:

104653270154306099169/

Google + CoderzHeaven

Android phpmySQL connection redone.

Hi all..
Most of our visitors had problem with one of our previous post on Android-php connection. So here I am posting another simple example to connect with a php file. Here I am simply putting an echo in the php file which ehoes back the data from the android side.
Here are the things you need to remember.

1. Make sure your server is running(Xampp or Wampp).
2. Make sure you give right path of your php file inside your android program.
3. Make sure you have put the internet permission in the android manifest file.
4. And the last one the android side and php side posting variables should be the same.

Here is the android main java file contents.

package pack.coderzheaven;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidPHPConnectionDemo extends Activity {
	Button b;
    EditText et;
    TextView tv;
    HttpPost httppost;
    StringBuffer buffer;
    HttpResponse response;
    HttpClient httpclient;
    List<NameValuePair> nameValuePairs;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        b = (Button)findViewById(R.id.Button01);
        et= (EditText)findViewById(R.id.EditText01);
        tv= (TextView)findViewById(R.id.tv);
 
        b.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            	
            	final ProgressDialog p = new ProgressDialog(v.getContext()).show(v.getContext(),"Waiting for Server", "Accessing Server");
            	Thread thread = new Thread()
            	{
            	    @Override
            	    public void run() {
            	    	 try{
                         	
                             httpclient=new DefaultHttpClient();
                             httppost= new HttpPost("http://10.0.0.2/my_folder_inside_htdocs/connection.php"); // make sure the url is correct.
                             //add your data
                             nameValuePairs = new ArrayList<NameValuePair>(1);
                             // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
                             nameValuePairs.add(new BasicNameValuePair("Edittext_value",et.getText().toString().trim()));  // $Edittext_value = $_POST['Edittext_value'];
                             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);
                             runOnUiThread(new Runnable() {
                            	    public void run() {
                            	    	p.dismiss();
                            	    	 tv.setText("Response from PHP : " + response);
                            	    }
                            	});
                            
                         }catch(Exception e){
                        	 
                        	 runOnUiThread(new Runnable() {
                         	    public void run() {
                         	    	p.dismiss();
                         	    }
                         	});
                             System.out.println("Exception : " + e.getMessage());
                         }
            	    }
            	};

            	thread.start();
            	
               
            }
        });
    }
}

Here is the main.xml the layout for the above file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<EditText
	android:text=""
	android:id="@+id/EditText01"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:singleLine="true">
</EditText>
<Button
	android:text="Send to php side"
	android:id="@+id/Button01"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content">
</Button>
<TextView
	android:id="@+id/tv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text=""
    />
</LinearLayout>

Here is the android manifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="pack.coderzheaven"
      android:versionCode="1"
      android:versionName="1.0">
      <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AndroidPHPConnectionDemo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Here is the PHP side. I am simply echoing back the value send from the android side.
connection.php

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

here I have a Edittext on my android side and after entering something in it and hitting the button to send, the textbox data will be send to the server and the server echoes back the same data.
My php file is saved in a folder named “my_folder_inside_htdocs” and named “connection.php”
I am working in localhost so it is given as “10.0.2.2” please replace your domain name here.
However you have a MySQl-connection in that php file and fetch some data from the database and echo back from the php side. You will get that data as string in the php side in this line

ResponseHandler<String> responseHandler = new BasicResponseHandler();
					String response = httpclient.execute(httppost, responseHandler);
					System.out.println("Response : " + response); </blockquote>

You can manipulate this string as you want.
Any problem with this post please leave your comments.If this post was useful then click on the +1 button on top to share it across the web.
Please leave your valuable comments.

Here is another more detailed explanation.

How to create Simple Login form using php in android? – Connect php with android.