【问题标题】:Sending data from android to php using http使用http将数据从android发送到php
【发布时间】:2014-04-02 05:33:34
【问题描述】:

这是我用来发送到网站中我的 php 文件的我的 android 代码。还修改了android清单文件。

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(7);
        nameValuePairs.add(new BasicNameValuePair("number", "5556"));
        nameValuePairs.add(new BasicNameValuePair("password",password));
        nameValuePairs.add(new BasicNameValuePair("token",token));

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://futuretime.in/post.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);

这是我的 php 脚本,用于从 android 接收数据。 但我的问题是当我在 php 中打印数据时,它没有显示任何值。

        <?php 
          $number= $_POST['number'];
          $password = $_POST['password'];
          $token   = $_POST['token'];
          echo $number;
        ?>

【问题讨论】:

  • 您的问题是什么?你有什么错误吗?
  • 你到底想做什么???
  • 可以通过JSON解析发送数据。小参考是stackoverflow.com/a/22686616/3360307
  • 从客户端调用时无法在 php 中回显数据,从 android 调用时将数据保存在某处,然后查看是否接收到它
  • if (!empty($_POST)) {$number= $_POST['number']; $password = $_POST['密码']; $token = $_POST['token']; echo $number;}else{echo "Empty";} 试试这个并检查您的帖子是否为空。然后您可以确定错误在哪里。

标签: java php android http


【解决方案1】:

你可以尝试使用 Ion https://github.com/koush/ion ,这是一个很好的 http 请求库。 是一个简单的示例。查看项目站点以获取更多示例和 wiki。

    Ion.with(getContext(), "https://koush.clockworkmod.com/test/echo")
   .setBodyParameter("goop", "noop")
   .setBodyParameter("foo", "bar")
   .asString()
   .setCallback(...)

【讨论】:

    猜你喜欢
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    相关资源
    最近更新 更多