1

我已经创建了一个 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));

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

      [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"];

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

4

3 回答 3

3

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

于 2013-08-06T11:36:54.940 回答
0

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

          $lat=$_POST['latitude'];
          $long=$_POST['longitude'];
          $speed=$_POST['speed'];
          $time=$_POST['time'];
于 2013-08-06T11:52:57.417 回答
0

将您的值放在一个数组中并从 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();
于 2013-08-06T12:27:39.657 回答