Java - Android无法使用httpPost发送数据

标签 java php android post http-post

我在 Android 中使用 httpPost 发送数据时遇到问题。我找到了一些示例,并且没有任何错误或异常,但在 php 站点上 $_POST 始终为空/null。

这是我的代码:

HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://xxxxxxx.com/test.php");

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("v", "123"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(httppost);
        String responseText = EntityUtils.toString(response.getEntity());
        Toast.makeText(this, responseText, 5000).show();
        System.out.println(responseText);

    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }

PHP:

<?php $t=$_POST['v']; print $t; ?>

并且它不打印 123...

添加:

如您所见,有一个字符串responseText。 在该字符串中,我可以看到有我想要打印的“123”。

最佳答案

<?php $t=$_POST['v']; print $POST['v']; ?>

您的打印内容有拼写错误(缺少“_”)。

将 print $POST['v']; 更改为 print $_POST['v']; 或简单地 print $t;

关于Java - Android无法使用httpPost发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9976506/

相关文章:

android - 使用一个大包还是多个小包更好?

Android:如何将 Studio 连接到 Genymotion 虚拟设备?

android - ImageView 上的 TranslateAnimation (Android)

java - Scala: 'val' 没有初始化

java - AWS Java 开发工具包 : Specifying KMS Key Id For EBS

java - 如何使用showMessage()方法?

php - Docker安装cURL失败(需要7.10.5以上版本)

php - strtotime ('-1 month' ) 如果月份有 31 天则返回错误的日期

java - 减少字符数组的长度

来自字符串的 Php 正则表达式 "deleting only one charactered words"