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.

Android phpMysql connection

First create a database  named “mydatabase” in MySql. Then create a table “tbl_user” with three fields (id, username and password).

The next step is to create a php page which will communicate between the  MySql database and the android application.

For this create a “Connections.php” page as shown below. It will set up a  connection to the database with the username and password

$hostname_localhost ="localhost";
$database_localhost ="mydatabase";
$username_localhost ="root";
$password_localhost ="";

$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);

Then the main PHP file is to be created. Actually the android application will call this php file along with the data(username and password ) and this code will check the database. If the details is valid then it will return “Y” and otherwise return “N”.

Here the data is passed as POST method. We can also use GET method where the data is passed along with the url . Android support both these two features.

<?php require_once('yourfolder/Connections.php'); mysql_select_db($database_localhost,$localhost);

$useremail = $_POST['UserEmail'];
$password = $_POST['Password'];

 $query_search = "select * from tbl_user where username = '".$useremail."' AND password = '".$password. "'";
 $query_exec = mysql_query($query_search) or die(mysql_error());
 $rows = mysql_num_rows($query_exec);

 if($rows --> 0) { echo "Y"; }
else  {echo "N"; }

So by now we created php and the database part. Then we have to focus on android section. For this first create a xml file which will be our login screen.The screen look like this.

The layout of the xml is displayed below.

<RelativeLayout
	  xmlns:android="http://schemas.android.com/apk/res/android"
	  android:layout_width="fill_parent"
	  android:layout_height="fill_parent"
	  android:background="#ff2a1703"
	  android:fadingEdge="horizontal">
	 <TextView
	 		android:id="@+id/text"
	 		android:text="manage your projects..."
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:typeface="serif"
            android:textStyle="italic"
            android:layout_centerHorizontal="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="50dip"
            android:textSize="20sp" >
     </TextView>
	 <EditText
			android:id="@+id/username"
			android:layout_width="213dip"
			android:layout_marginTop="60dp"
			android:layout_below ="@+id/text"
			android:layout_centerHorizontal="true"
			android:layout_height="wrap_content"
			android:hint="username"
			android:gravity="center"
			android:textSize="18sp"
			android:typeface="sans"
			android:textStyle="italic">
	 </EditText>
	 <EditText
			android:id="@+id/password"
			android:layout_width="211px"
			android:layout_height="wrap_content"
			android:hint="password"
			android:textSize="18sp"
			android:typeface="sans"
			android:textStyle="italic"
			android:gravity="center"
			android:password="true"
			android:layout_marginTop="20dp"
			android:layout_below ="@+id/username"
			android:layout_centerHorizontal="true">
	 </EditText>
 	 <CheckBox
			android:id="@+id/check"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="Remember me"
			android:layout_marginTop="10dp"
			android:layout_below ="@+id/password"
			android:layout_centerHorizontal="true">
	 </CheckBox>
	 <Button
			android:id="@+id/login"
			android:layout_width="141px"
			android:layout_height="wrap_content"
			android:text="Login"
			android:textSize="22sp"
			android:typeface="serif"
			android:textStyle="bold"
			android:layout_marginTop="20dp"
			android:layout_below ="@+id/check"
			android:layout_centerHorizontal="true">
	 </Button>
</RelativeLayout>

We now create the java file . This file should contain the 3 sections

1. Store the preference when the checkbox is checked
2. Establish a connection between this file and the php file
3. Validate the user

package com.ar.mydatabaseProject.pack;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class mydatabaseProject extends Activity
{
    /** Called when the activity is first created. */

	Button login;
	String name="",pass="";
	EditText username,password;
	TextView tv;
	byte[] data;
	HttpPost httppost;
	StringBuffer buffer;
	HttpResponse response;
	HttpClient httpclient;
	InputStream inputStream;
	SharedPreferences app_preferences ;
	List<NameValuePair> nameValuePairs;
	CheckBox check;
	public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        login = (Button) findViewById(R.id.login);
        check = (CheckBox) findViewById(R.id.check);

        String Str_user = app_preferences.getString("username","0" );
	    String Str_pass = app_preferences.getString("password", "0");
	    String Str_check = app_preferences.getString("checked", "no");
        if(Str_check.equals("yes"))
        {
        		username.setText(Str_user);
        		password.setText(Str_pass);
        		check.setChecked(true);
        }
        login.setOnClickListener(new View.OnClickListener()
        {
			public void onClick(View v)
			{
				name = username.getText().toString();
				pass = password.getText().toString();
				String Str_check2 = app_preferences.getString("checked", "no");
				if(Str_check2.equals("yes"))
				{
					SharedPreferences.Editor editor = app_preferences.edit();
					editor.putString("username", name);
					editor.putString("password", pass);
					 editor.commit();
				}
				if(name.equals("") || pass.equals(""))
				{
					 Toast.makeText(mydatabaseProject.this, "Blank Field..Please Enter", Toast.LENGTH_LONG).show();
				}
				else
				{


			    try {
			    	httpclient = new DefaultHttpClient();
				    httppost = new HttpPost("http://10.0.2.2/android/user_validate.php");
			        // Add your data
			        nameValuePairs = new ArrayList<NameValuePair>(2);
			       nameValuePairs.add(new BasicNameValuePair("UserEmail", name.trim()));
			        nameValuePairs.add(new BasicNameValuePair("Password", pass.trim()));
			        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

			        // Execute HTTP Post Request
			        response = httpclient.execute(httppost);
			        inputStream = response.getEntity().getContent();

			        data = new byte[256];

			        buffer = new StringBuffer();
	                int len = 0;
	                while (-1 != (len = inputStream.read(data)) )
	                {
	                    buffer.append(new String(data, 0, len));
	                }

	                inputStream.close();
			    }

			    catch (Exception e)
			    {
			    	Toast.makeText(mydatabaseProject.this, "error"+e.toString(), Toast.LENGTH_LONG).show();
			    }
			    if(buffer.charAt(0)=='Y')
			    {
			    	Toast.makeText(mydatabaseProject.this, "login successfull", Toast.LENGTH_LONG).show();
			    }
			    else
			    {
			    	Toast.makeText(mydatabaseProject.this, "Invalid Username or password", Toast.LENGTH_LONG).show();
			    }
				}
			}
		});
        check.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                // Perform action on clicks, depending on whether it's now checked
            	SharedPreferences.Editor editor = app_preferences.edit();
                if (((CheckBox) v).isChecked())
                {


                	 editor.putString("checked", "yes");
                	 editor.commit();
                }
                else
                {
                	 editor.putString("checked", "no");
                	 editor.commit();
                }
            }
        });
    }
        public void Move_to_next()
        {

        //	startActivity(new Intent(this, zzz.class));
        }
}

At last don’t forget to put the permission in Manifest file

<uses-permission android:name="android.permission.INTERNET"></uses-permission>


Note : if you have any problem in working with this post, please check this more simple version here.

Here is another more detailed explanation.

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