在Android HTTP POST [英] HTTP POST on Android

查看:136
本文介绍了在Android HTTP POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想作一个简单的HTT prequest PHP脚本,我试图让很大部分基本的应用程序,以获得功能性的工作。我想测试我的应用程序发送我喂养它,所以我所做的Andr​​oid应用程序发送到服务器的数据和服务器应该送我,我已经投入了应用程序中的数据。在code为POSTDATA功能是从Android的数据发送到服务器和code为default.php是在Web服务器的recieving php文件,然后将数据转发到我的电子邮件地址(没有给)。这里是code

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
    }
}

以及用于default.php。

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>");
}
?>

下面是在code在引擎收录的链接: POSTDATA() default.php

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

我收到的电子邮件发送但是the​​name和theage的数据似乎是空的。收到确切的电子邮件是 你好吗,? 在你变老了。 这表明,我认为服务器发送thename和theage为空。之前有任何的你试过吗?我在做什么错误? 非常感谢您抽出时间来阅读我的code和可能的话回答我的问题。

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.

编辑:非常愚蠢的我 - >的被需要更改为POST。离开这里用于存档:)

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

推荐答案

您使用的是 $ _ GET 而不是 $ _ POST 获得发布的数据值。你想

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

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

这篇关于在Android HTTP POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆