【问题标题】:Getting Post data in PHP from Android从 Android 获取 PHP 中的 Post 数据
【发布时间】:2013-08-06 11:34:25
【问题描述】:

我创建了一个 http post 请求,用于在 android 中连接 PHP 服务器。 但在服务器端我无法从数组中提取数据

这是发出http请求的代码

      List<NameValuePair> pairs = new ArrayList<NameValuePair>(2);
    pairs.add(new BasicNameValuePair("time",
        String.valueOf(location.getTime())));
 pairs.add(new BasicNameValuePair("latitude", new DecimalFormat("#.######").format(location.getLatitude())));
    pairs.add(new BasicNameValuePair("longitude",
            new DecimalFormat("#.######").format(location.getLongitude())));
    pairs.add(new BasicNameValuePair("speed",
        String.valueOf(location.getSpeed())));

   HttpPost post = new HttpPost(endpoint);
   post.setEntity(new UrlEncodedFormEntity(pairs));

在 Eclipse 中,我记录了所有的值。它正在打印 我调试“对”它会打印一个数组

      [locations[0][time]=1375788271891,
       locations[0][latitude]=12.966116, 
       locations[0][longitude]=77.638493,
       locations[0][speed]=0.0]

在 php 中,我尝试使用

获取此数据
              $lat=$_POST["latitude"];
              $long=$_POST["longitude"];
              $speed=$_POST["speed"];
              $time=$_POST["time"];

但我没有得到这些值。有什么问题?有没有人可以帮助我..请回复.. 提前感谢:)

【问题讨论】:

  • @Nirmal 实际上我的问题是 - 从 android 发送的数据是一个数组 location[][]==xxxx 如何从该数组中提取这些数据。那是我的问题
  • 尝试跟随桑德斯的回答。您应该将参数转换为 JSON。
  • 您能否对您提交的数据进行 var_dump($_POST) 并向我们展示结果。我不认为你可以在不先序列化的情况下发送那种类型的数据

标签: php android arrays http-post


【解决方案1】:

您可以尝试将参数转换为 JSON,然后将它们发布到 PHP。

【讨论】:

  • 我们如何将其转换为 json?可以举个例子吗?
【解决方案2】:

我做过类似的事情,我猜你知道如何通过 php 将数据发送到服务器, 试试这个,在你的 php 中,

          $lat=$_POST['latitude'];
          $long=$_POST['longitude'];
          $speed=$_POST['speed'];
          $time=$_POST['time'];

【讨论】:

  • 我尝试了相同的代码 yar..但问题出在 php 中,它的数据是以数组的格式获取的。 [位置[0][时间]=1375788271891,位置[0][纬度]=12.966116,位置[0][经度]=77.638493,位置[0][速度]=0.0]
  • 然后尝试首先在变量中获取您的值,然后传递它。
  • 你的权利..我们如何在 php 中获取所有帖子数据?你知道?之后我们可以提取每个值。对吗?
  • 或者你可以用java提取数据,然后传递给php,那么POST数据就不会有问题了。
【解决方案3】:

将您的值放入一个数组并从 PHP 端回显它们。

$arr = array('Name'=> 'Values');
echo json_encode($arr);

您可以在 Android 端尝试以下代码,“结果”是一个包含 JSON 表单结果的字符串,您可以使用 JSONObject 和 JSONArray 类对其进行解析:

    HttpPost httppost = new HttpPost(KEY);
            HttpParams httpParameters = new BasicHttpParams();

            if(nameValuePairs!=null)
            {
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            }

            // Set the timeout in milliseconds until a connection is established.
            // The default value is zero, that means the timeout is not used. 
            int timeoutConnection = 8000;
            HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
            // Set the default socket timeout (SO_TIMEOUT) 
            // in milliseconds which is the timeout for waiting for data.
            int timeoutSocket = 8000;
            HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

            //Still dont know the reason why this code was implemented, will have to find out that!!
            HttpClient httpclient = new DefaultHttpClient(httpParameters);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

            //Reads the PHP output of JSON and writes it into a String using StringBuilder
            BufferedReader buffer = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();

            while ((line = buffer.readLine()) != null)
            {
                sb.append(line + "\n");
            }

            result = sb.toString();

【讨论】:

    猜你喜欢
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多