我的数据存储在 HashMap 中,我想将此数据传递到 URL,但我希望它保持分组在一起。我正在考虑序列化 HashMap,然后将该数据传递给 PHP,然后取消序列化。我的第二个选择是将数据传递到 JSON,然后将其传递到 url。
有人对此有任何建议或更好的解决方案吗?
示例:
//Has Data added to hash map
HashMap hashmap1=new HashMap();
//Has Data added to hash map
HashMap hashmap2=new HashMap();
//dateEncoded Method
//Will encode data by serializing it or passing it to JSON
String data = URLEncoder.encode(dataEncoded(hashmap1), "UTF-8") + "=" + URLEncoder.encode("10", "UTF-8");
data += "&" + URLEncoder.encode(dataEncoded(hashmap2), "UTF-8");
// Send data
URL url=new URL("http://localhost/index.php?"+data);
最佳答案
只要您假设找到的所有对都需要出现在 map 中,参数名称/值对就可以正常工作。
关于java - 将 HashMap 值传递给 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6485751/