我正在尝试使用 J2ME 从 URL 中提取查询的名称-值对,但这并不容易。 J2ME 没有 java.net.URL 类,String 也没有 split 方法。
有没有办法使用 J2ME 从 URL 中提取名称-值对?任何开源实现也将受到欢迎。
最佳答案
我喜欢kchau回答,但我只是将数据结构从两个数组更改为一个哈希表。如果 URL 参数的数量未知,这也会有所帮助。
String url = "http://www.so.com?name1=value1&name2=value2&name3=value3";
Hashtable values = new Hashtable();
int s = url.indexOf("?");
int e = 0;
while (s != -1) {
e = url.indexOf("=", s);
String name = url.substring(s + 1, e);
s = e + 1;
e = url.indexOf("&", s);
if (e < 0) {
values.put(name, url.substring(s, url.length()));
} else {
values.put(name, url.substring(s, e));
}
s = e;
}
for (Enumeration num = values.keys(); num.hasMoreElements();) {
String key = (String)num.nextElement();
System.out.println(key + " " + values.get(key));
}
关于java - 如何在 J2ME 中解析 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/463482/