从Android获取PHP发布数据

时间:2013-08-06 11:34:26

标签: php android arrays http-post

我创建了一个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"];

但我没有得到价值观。有什么问题?有人可以帮我..请回复.. Thanx in Advance:)

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

将您的值放在数组中并从PHP端回显它们。

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

您可以尝试以下Android Side代码,“result”是一个包含JSON表单结果的String,您可以使用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();