gpt4 book ai didi

php - Android 上的 HTTP POST

转载 作者:太空宇宙 更新时间:2023-11-03 11:52:08 24 4
gpt4 key购买 nike

我想对 php 脚本发出一个简单的 HTTPRequest,并且我尝试制作最基本的应用程序来使功能正常工作。我想测试我的应用程序是否正在发送我提供的数据,所以我已经将 android 应用程序发送到服务器,并且该服务器应该将我放入应用程序的数据发送给我。 “postData”函数的代码是将数据从 android 发送到服务器,“default.php”的代码是网络服务器上的接收 php 文件,然后将数据转发到我的电子邮件地址(未给出)。这是代码

    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”

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

以下是 pastebin 中代码的链接: postData()default.php

我收到一封电子邮件,但是发送的数据“thename”和“theage”似乎是空的。收到的确切电子邮件是“你好,你好吗,?在你变老的时候。”这向我表明服务器将 thename 和 theage 发送为空。你们有没有试过这个?我做错了什么?非常感谢您抽出时间阅读我的代码,并在可能的情况下回答我的问题。

编辑:我太白痴了 -> “获取”需要更改为“发布”。将它留在这里用于存档目的:)

最佳答案

您正在使用 $_GET 而不是 $_POST 来获取已发布数据的值。你要

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

关于php - Android 上的 HTTP POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10246068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com