简体   繁体   English

如何将 HashMap 的 Arraylist 发送到 _POST? 然后我如何剖析 PHP 中的数组?

[英]How can I send an Arraylist of HashMaps to _POST? And then how can I dissect the array in PHP?

I'm writing an Android app which collects user information and ultimately stores that in mySQL.我正在编写一个 Android 应用程序,它收集用户信息并最终将其存储在 mySQL 中。 To add a new user, I'm sending _POST data to a PHP script from Android using:要添加新用户,我使用以下命令将 _POST 数据从 Android 发送到 PHP 脚本:

Uri.Builder builder = new Uri.Builder()
       .appendQueryParameter("id",params.get("id"))
       .appendQueryParameter("name",params.get("name"))
       .appendQueryParameter("email",params.get("email"));

String query = builder.build().getEncodedQuery();

On the PHP side, I'm receiving the _POST data and inserting to mySQL using:在 PHP 方面,我正在接收 _POST 数据并使用以下命令插入到 mySQL:

$id = $_POST["id"];
$name = $_POST["name"];
$email = $_POST["email"];
$query = "INSERT INTO users(id,name,email) VALUES('$id','$name','$email')"; 

Simple stuff.简单的东西。 However, I would also like to bulk add thousands of such records in one shot.但是,我还想一次性批量添加数千条此类记录。

No problem on the Java side: I have an Java 方面没问题:我有一个

Arraylist<HashMap<String,String>> 

to hold these thousands of 'rows' of user data.保存这些数千“行”的用户数据。

However, how can I pass this Arraylist of HashMaps to _POST?但是,如何将这个 HashMaps Arraylist 传递给 _POST? In turn, on the PHP side, how can I dissect the lengthy _POST data (I would imagine breaking down an array) and write to the mySQL database?反过来,在 PHP 方面,我如何剖析冗长的 _POST 数据(我想分解一个数组)并写入 mySQL 数据库?

I've not found a specific example of this on SO, on the Java end or the PHP end.我没有在 SO、Java 端或 PHP 端找到一个具体的例子。

Thanks to the power of 3.感谢3的力量。

You could try to use an interchangeable format for that, JSON for example.您可以尝试为此使用可互换的格式,例如JSON

Convert a Java hashmap to a JSON object through: new JSONObject(map);通过以下方式将 Java 哈希映射转换为 JSON 对象: new JSONObject(map);

Then you could decode it in PHP, either manually or through an existing function, eg json_decode() .然后您可以手动或通过现有函数(例如json_decode()在 PHP 中对其进行解码。

Is this what you are looking for?这是你想要的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM