HTTP POST on Android

自闭症网瘾萝莉.ら 提交于 2019-12-01 07:10:22

问题


I want to make a simple HTTPRequest to a php script and I have tried to make the very most basic of apps to get the functionality working. I want to test that my app is sending the data I feed it so I've made the android app send to a server and that server is supposed to send me the data I've put into the app. The code for the "postData" function is to send data from android to the server and the code for the "default.php" is the recieving php file on a webserver which then forwards the data to my email address (not given). 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 "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

I recieve an email however the data sent "thename" and "theage" seem to be empty. The exact email received is "Hi, How are you,? At you are getting old." which indicates to me that the server is sending thename and theage as empty. Have any of you tried this before? What am I doing incorrectly? Thank you so much for taking the time out to read my code and if possible to reply to my questions.

EDIT: Very idiotic of me -> the "GETS" need to change to "POST". Leaving it here for archive purposes :)


回答1:


You're using $_GET instead of $_POST to get the values of the posted data. You want

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



回答2:


you are sending a POST request but your php script uses GET variables

try

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



回答3:


The method used in both Android and PHP should the same, either GET or POST.

In this instance you can either change the PHP to this -

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

Or the chang ethe Android side to this -

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


来源:https://stackoverflow.com/questions/10246068/http-post-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!