Android HTTPPost Null - php

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.

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.

406 not acceptable when using HttpPost on android

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?

Android : HTTPS Connection to my database

I Used to make my connection to php for mysql with HTTP, Now I am asked to Use HTTPS as it is more secure. but i tried to many ways but can't get the tablet to POST or GET any information, I made a self signed certificate and added to Local Computer trusted zone so i wont be asked that its is not verified do i want to continue, i tried connecting by browser and it worked fine and printed all the info that i needed, but not through the app. i attached my Previous HTTP code that i need to change to HTTPS. would like some help to change this connection to HTTPS.
httpclient = new DefaultHttpClient();
httppost = new HttpPost ("http://xx.xx.xx.xx/E-MENU/login.php");
username = etUser.getText().toString();
password = etPass.getText().toString();
password = md5(SHA1(password));
try{
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode() == 200){
entity = response.getEntity();
if (entity!= null){
InputStream instream = entity.getContent();
JSONObject jsonResponse = new JSONObject (convertStreamToString(instream));
thanks Upfront.
So it works when you use HTTP but not HTTPS? The problem will be caused by the self-signed certificate on the server not being trusted by Android.
See the accepted answer for this question: Self-signed SSL acceptance on Android

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.

Categories