如何将一组数据从Android发送到php服务器


How to send an array of data from Android to php server?

对于单对值,我使用了:

datas.add(new BasicNameValuePair("latitude", Double.toString(lastKnownLocation.getLatitude())));

然后使用HttpClient:将其发送到服务器

HttpClient httpclient = CustomHttpClient.getHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(datas));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
input = entity.getContent();

在服务器端,我提取这些信息如下:

$latitude = (_POST['latitude']);

但对于我自己类型的数组,Place说有3个字段:

class Place {
     int id;
     String name;
     String address;
}

如何将这些信息发送到我的php脚本?在我的php中,如何提取这个JSON数组?

传输数据的方法数不胜数,但其中一种更常见的数据交换方法是通过JSON。

请参阅此问题了解如何做到这一点。简而言之,你的

  • 将JSON支持添加到您的";豆子;或者您想要传输的元素
  • 将相关bean转换为JSON,以及
  • 向远程服务器发送HTTP请求,包括JSON数据

请参阅qklxtlx的响应,了解如何在服务器端解释JSON。