Android 将listview中的所有数据发送到服务器的最佳方式是什么?
如何将列表视图中的所有数据发送到服务器我可以将它们作为文本一起发送还是逐个发送,以及如何发送?我们需要更多信息来帮助您!哪个服务器?哪些接口有服务器 但它建议您在服务器端使用Web服务(如REST),并从android设备通过HTTP调用它 更新1: 使用此方法,可以将字符串数组发送到localhost/index.php。这会将参数作为POST发送Android 将listview中的所有数据发送到服务器的最佳方式是什么?,android,Android,如何将列表视图中的所有数据发送到服务器我可以将它们作为文本一起发送还是逐个发送,以及如何发送?我们需要更多信息来帮助您!哪个服务器?哪些接口有服务器 但它建议您在服务器端使用Web服务(如REST),并从android设备通过HTTP调用它 更新1: 使用此方法,可以将字符串数组发送到localhost/index.php。这会将参数作为POST发送 public void sendArrayTOBackend(String[] listViewItems) { Gson gson =
public void sendArrayTOBackend(String[] listViewItems) {
Gson gson = new Gson(); //Json-Lib form google
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/index.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("data", gson.toJson(listViewItems)));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
public void sendaraytobackend(字符串[]listViewItems){
Gson Gson=new Gson();//谷歌格式的Json库
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://localhost/index.php");
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“数据”,gson.toJson(listViewItems));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
}
您可以像这样进入后端:
<?php
if(exists($_POST['data'])){
$listViewItems = json_decode($_POST['data']);
foreach ($listViewItems as $listViewItem) {
echo $listViewItem;
}
}
?>
以JSON格式发送。Xampp在我的计算机上,问题是我可以在php my admin上将所有列表视图记录作为单个记录发送吗?您可以从列表视图的所有值构建一个数组,然后将其转换为JSON,这样它就是一个简单的字符串,可以发送到后端@userr12请看一下我回答中的更新1