Android querying external MySQL database - php

I had some problem understanding the following line of code. What exactly does it do and how does it help me query the database from android
$q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
I know the initial part of it means select every record from table people where birthyear...
But after that I get confused. I am calling this php function from android using http post
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1980"));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/retrieve.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Where is an input stream. Can someone help me out?

The PHP code queries the database and constructs a JSON output, which it sends to the calling client. Try calling the php from a browser (put the URL in the address bar) - and you should get the JSON back.
On the Android side, java code is using HTTPClient wrapper to call your php and get the response. Socket (HTTP connection) InputStream is handled internally by HttpClient when you call relevant methods. From your point of view, all you need to do is deal with the content of the entity.
You can just open the input stream from the entity (your is variable) and read the data from it:
InputStream is = entity.getContent();
then read the content of the stream as you normally would. You should get the json as was sent by the php.
PHP gets the value of required minimum year from year request parameter. In android, a NameValuePair is created with name=year and value=1980. This NameValuePair is passed as POST parameter in the form year=1980 to the PHP code. Again, this is handled internally by the HTTPClient wrapper class. Then the PHP code reads the value of request parameter "year" to get the minimum value for the query.

In the above code is is the instance of InputStream entity.getContent() returns InputStream type object. In the line is = entity.getContent(); is is the InputStream

Related

Get Data from server

I managed to save all data to server using this code
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("www.URL.com");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
Now i want to get those data in my app .
i have 1 table in the server which has 4 rows
i want to get rows using row id.
Please help me with both server and android code to get saved data from server.
You'll need a code on the server that pulls the data from the database, and in Android you'll need code to call the function that pulls the data from the database.

Android to PHP error- Unidentified Index

when sending data to a php server through android I cam across this error: "Notice: Undefined index: IT in C:\xampp\htdocs\hello.php on line 4". After a few hours of messing around with it I can't solve my issue.
hello.php :
<?php
mysql_connect("localhost","user","pass");
mysql_select_db("mmo");
$r=mysql_query("update players set X = '".$_REQUEST['IT']."' where 22=22");
if(!$r)
echo "Error in query: ".mysql_error();
mysql_close();
header('Refresh: 0.01; URL=http://localhost/hello.php');
?>
update method in android:
public void UpdateSeverWithPlayer()
{List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("IT","3"));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/hello.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}}
I am working on the android emulator and do have the internet permission. As always help is greatly appreciated.
EDIT 1: It appears the issue is in the android and not the php code.
You're not sending your query string propertly somehow, so there is no $_REQUEST['IT'] on the PHP side. do a var_dump($_REQUEST) to see what's coming through. Or better yet, don't use $_REQUEST. It's somewhat insecure. Better use the superglobal directly related to your HTTP method, _GET or _POST.
As well, the Refresh header is non-standard. it'll work, but you shouldn't depend on it. An HTTP level redirect uses the Location: header. Refresh: is a non-standard old-school Netscape extension.

Get POST data on Webpage from Android-App?

I am trying to output POST-data that I send from an android phone, however I cannot manage to get any output in my PHP file.
This is my php code:
<?php
echo "POSTed data: '".$_POST['mydata']."'<br>";
var_dump($_POST);
?>
this is my android code (which seems to work fine)
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("myurl.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("mydata", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringData", "SomeData"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
String responseText = EntityUtils.toString(response.getEntity());
toSendET.setText(responseText);
Is it even possible to send POST data to an static PHP script via phone?
What you have got is working fine - but when you echo something in PHP its echo'd back to the caller - ie the browser in this case.
If you check the value of response in the code on Android is will be POSTed data: '12344'<br>
See the error_log() function in PHP for logging to a file

How do I send data in a url to test a php file?

I am trying to see what is wrong with a php script a wrote. The script is getting information from an android application and then using that data to search a table and send information back. The client side is written like this:
nameValuePairs.add(new BasicNameValuePair("num1", num1));
nameValuePairs.add(new BasicNameValuePair("num2", num2));
nameValuePairs.add(new BasicNameValuePair("num3", num3));
nameValuePairs.add(new BasicNameValuePair("num4", num4));
InputStream is = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.0.4/xampp/phpfile.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
How do I format the url in a browser if I want to execute the same script passing num1, num2, num3 and num4 in the same way?
use http://hurl.it/ to test POST request from your browser...
Since you are using POST methos, as I can see, you can't call it from a browser url.
You can write a simple html file, with a form in it, wich uses post method, and then submit that.
Or modify the server side php file, and instead of using $_POST[] variables use $_REQUEST[] variables, wich accept both GET and POST method calls.
In this case:
http://10.0.0.4/xampp/phpfile.php?num1=num1&num2=num2...
should work fine.

server client in android

I try to send some request from android emulator to a php server. I tried this code.
HttpPost post = new HttpPost(url);
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
InputStream in=resEntity.getContent();
in.close();
here I need to read some xml data as inputstream. But I don't have enough knowledge in php. So can you explain me how looks like my php code to send xml data to android emulator.
The simplest way is to have PHP print the xml data, if it's being dynamically generated.
If it's a static XML file then there's no reason to bother with PHP, just store the file on the server and retrieve the file.

Categories