Posts Tagged Android to php

Save data to php server from android

Actually to transfer data from android to php file, we need 3 things

1.phpFileName(where coding is written to retrieve data) i.e. server side coding
2.array of data(which is being send from android file)
3.no. of data in that array

Let those things are:
1.a) datas which are going to php file are: name ,password,location .Put it inside an String array named as parameters.
b) its count is 3.
c) file name of php page userjson.php

String[] parameters = {mName,mPassword,mLocation};

2.Lets create an object of a class HttpPostClass where logic is written to send the above data .Send all the data with help of constructor.

 HttpPostClass httpPostClass = new HttpPostClass("userjson.php", 3,parameters);

3.Now call HttpPostClass method doPost which returns HttpResponse object.

   HttpResponse response = httpPostClass.doPost();

Now we are making a class where a reusable method is defined.Here you will need all the three things which are mentioned above.
For this, we will need:

Make a method doPost which returns HttpResponse .Let it be in class named as HttpPostClass.

And make a constructor in the class to instantiate the three variables -phpPage,noOfParameters,parameters.

In the method doPost.

1.Define

HttpResponse httpResponse=null;

2.make new reference to

 HttpClient client=new DefaultHttpClient();

3.Define databaseurl which is the exact address of your php file location

i.e.

 String databaseurl="http://10.0.2.2/localFolder/"+phpPage;

4.Make a new array list with the number of parametrers

List nameValuePairs=new ArrayList(noOfParameters);

5.Now it is the time to use the data which is inside string array named as parameters

6.Now open the array and add its data one by one as an argument to BasicNameValuePair.

for(int i=0;i<noOfParameters;i++){

nameValuePairs.add(new BasicNameValuePair("item"+i, parameters[i]));

}

7.Now use this method

 httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

8.Finally

 httpResponse =client.execute(httpPost);

9.Thats it.You have successfully posted your data to php file.Now it is time for php coding.

10.How to retrieve those datas.


class HttpPostClass{

String phpPage;

int noOfParameters;

String[] parameters;

ReturningClass(String phpPage,int noOfParameters,String[] parameters){

this.phpPage=phpPage;

this.noOfParameters=noOfParameters;

this.parameters=parameters;<
 }

public HttpResponse doPost(){

HttpResponse httpResponse=null;

try{

HttpClient client=new DefaultHttpClient();

//phpPage is your php File name where You have written code to retrieve its data(i.e. data which is being send from this file)

String databaseurl="http://10.0.2.2/localFolder/"+phpPage;

HttpPost httpPost=new HttpPost(databaseurl);

List nameValuePairs=new ArrayList(noOfParameters);

for(int i=0;i&lt;noOfParameters;i++){

nameValuePairs.add(new BasicNameValuePair("item"+i, parameters[i]));

System.out.println(parameters[i]);

}

<strong>httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));</strong>

<strong>httpResponse =client.execute(httpPost);</strong>

}</strong>
 catch (Exception e) {

e.printStackTrace();

}

return httpResponse;
 }
 }

Now its turn of php coding..

.
How will it retrieve data???

make a php file named as userjson.php or whatever is the name.

<?php
$hostname_localhost ="localhost";
$database_localhost ="SocialTodos";
$username_localhost ="root";
<strong>$password_localhost ="";

$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost);

$username = $_POST['item0'];

$password = $_POST['item1'];

$location=$_POST['item2'];

mysql_select_db($database_localhost,$localhost);

//Do whatever u want with these data .I am just printing it.

echo $username;

echo $password ;

echo $location;

?>

Now dont get tensed .You have almost executed your php file

Come back to java file that is android file………Here you will retrieve the echo value of php file.

1.Make a new static method httpResponse inside HttpPostclass.

public static StringBuffer httpResponse(HttpResponse response){

StringBuffer buffer = new StringBuffer();</strong>
 try{
 InputStream inputStream = response.getEntity().getContent();

BufferedReader in=new BufferedReader(new InputStreamReader(inputStream));

String s;</strong>

while((s=in.readLine())!=null
 {
buffer.append(s);
}

inputStream.close();

}catch (Exception e) {

e.printStackTrace();
}</strong>

return buffer;

}

2.The HttpResponse recieved above in the starting will be used.

String echostring=HttpPostClass.httpResponse(response).toString();

, , , , ,

3 Comments