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.
Related
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.
I send a file via HTTPPost in android:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
In PHP how do I get the file?
I'm not really sure where your file would end up on the server. As far as I can tell you won't be able to access it using the $_FILES array.
I would suggest that you try to upload the file to your server the same way as it would be uploaded using an HTML form. If you do it that way you can handle the file upload using the $_FILES array. Judging from similar questions on SO there's a ton of code you need to write to get this to work though... BUT Android Asynchronous Http Client has good support for file uploads out of the box:
AsyncHttpClient client = new AsyncHttpClient();
File myFile = new File("/path/to/file.png");
RequestParams params = new RequestParams();
try {
params.put("profile_picture", myFile);
client.post("http://www.yourserver.com/upload.php", params, responseHandler);
}
catch(FileNotFoundException e) {
// handle
}
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
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.
I am trying to post a string to a php file on my web server from my android phone. I am able to establish a connection to the file, as I can retrieve what it prints. The problem I am having is sending the POST variable, which continually seems to be null(I am testing the program now, and sending back what the POST variable is). Here is the relevant code:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(HTTP://WWW....PHP FILE ADDRESS);
try{
// the data to send
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("RoomName", "HELLO"));//params[1]
// http post
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
My PHP code is (at the moment) as follows:
$roomname = $_POST['RoomName'];
$returnArray[] = array("LoggedIn" => $roomname);
print_r(json_encode($returnArray));
I have been looking at forums for the past week, and all the httppost examples follow this almost exactly. I would appreciate any help that you are willing to give. Thanks a lot
Well, try $_REQUEST in place $_POST.