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
Related
I want to using HttpPost to post data to my web site.
and here is my code
String st = "http://www.yade.cc/huli/classget/search.php";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(st);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("number", str));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params, HTTP.UTF_8);
post.setEntity(ent);
HttpResponse response = client.execute(post);
String retSrc = EntityUtils.toString(response.getEntity(),"utf-8");
but the result is:
406 Not Acceptable
An appropriate representation of the requested resource /huli/classget/search.php could
not be found on this server
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument
to handle the request.
when I add user-agent data into mycode, it works.
post.setHeader("User-Agent", "Chrome/28.0.1500.95");
but the result is something like
口口口口口口result口口口口
口means the char i can not see and i dont know what it is.
I try to use php to do the same thing, the result is totally ok, nothing strange appeared.
how to solve this problem?
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.