HTTP POST on Android

I want to make a simple HTTPRequest for php script, and I tried to make the simplest applications to get the functionality. I want to check that my application sends the data that I feed, so I sent the Android application to the server, and this server should send me the data that I put into the application. The code for the "postData" function is the transfer of data from android to the server, and the code for "default.php" is the resulting php file on the web server, which then sends the data to my email address (not specified). Here is the code

    public void postData() {

    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://somewhere.net/default.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("thename", "Steve"));
        nameValuePairs.add(new BasicNameValuePair("theage", "24"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        //HttpResponse response = httpclient.execute(httppost);

        // Execute HTTP Post Request
        ResponseHandler<String> responseHandler=new BasicResponseHandler();
        String responseBody = httpclient.execute(httppost, responseHandler);

    //Just display the response back
    displayToastMessage(responseBody);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
}

as well as for the parameter "default.php"

<?php 
$thename=$_GET["thename"]; 
$theage=$_GET["theage"];

$to = "someemail@gmail.com";
$subject = "Android";
$body = "Hi,\n\nHow are you," . $thename . "?\n\nAt " . $theage . " you are getting old.";
if (mail($to, $subject, $body)) 
{
echo("<p>Message successfully sent!</p>");
} 
else 
{
echo("<p>Message delivery failed...</p>");
}
?>

Here are the links to the code in pastebin: postData () and default.php

, , "thename" "theage", . ", ,? . , . - ? ? , , , , .

EDIT: → "GETS" "POST". :)

+3
3

$_GET $_POST, .

<?php 
$thename=$_POST["thename"]; 
$theage=$_POST["theage"];
+10

POST, php script GET

 $thename=$_POST["thename"]; 
$theage=$_POST["theage"];
+1

The method used for both Android and PHP should be the same: GETor POST.

In this case, you can either change PHP to this -

$thename = $_POST["thename"];
$theage = $_POST["theage"];

Or the Android side with Android movement -

HttpGet httppost = new HttpGet("http://somewhere.net/default.php");
-1
source

All Articles