4

我正在尝试使用 J2ME 从 URL 中提取查询的名称-值对,但这并不容易。J2ME 没有 java.net.URL 类,String 也没有 split 方法。

有没有办法使用 J2ME 从 URL 中提取名称-值对?任何开源实现也将受到欢迎。

4

7 回答 7

4

我喜欢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));
    }
于 2009-01-21T16:11:07.240 回答
2

由于 Java JDK 是开源的,您还可以从主 JDK 中借用 java URL 类并将其添加到您的项目中。这将允许您使用 Java SE 中的相同实现:

http://www.docjar.com/html/api/java/net/URL.java.html

于 2010-07-01T20:04:47.630 回答
2

这是我的尝试,与大卫的回答有些相似。

    String url = "http://www.stackoverflow.com?name1=value1&name2=value2&name3=value3";

    String[] names  = new String[10];
    String[] values = new String[10];

    int s = url.indexOf("?");  // Get start index of first name
    int e = 0, idx = 0;

    while (s != -1) {
        e = url.indexOf("=", s);              // Get end index of name string
        names[idx] = url.substring(s+1, e);
        s = e + 1;                            // Get start index of value string
        e = url.indexOf("&", s);              // Get index of next pair

        if (e < 0)  // Last pair
            values[idx] = url.substring(s, url.length());
        else        // o.w. keep storing
            values[idx] = url.substring(s, e);

        s = e;
        idx++;
    }

    for(int x = 0; x < 10; x++)
        System.out.println(names[x] +" = "+ values[x]);

测试了它,我认为它有效。希望对你有帮助,祝你好运。

于 2009-01-21T02:12:13.110 回答
1

在我的脑海中,它会变成这样(警告:未经测试):

String url = ...;
int s = url.indexOf("?") + 1;
while (s > 0) {
    int e = url.indexOf("=", s);
    String name = url.substring(s, e), value;
    s = e + 1;
    e = url.indexOf("&", s);
    if (e < 0)
        value = url.substring(s, e);
    else
        value = url.substring(s, e);
    // process name, value
    s = e;
}

查询字符串在技术上可以用分号而不是和号分隔,例如name1=value1;name2=value2;...,尽管我在实践中从未见过它。如果这对您来说是一个问题,我相信您可以修复它的代码。

于 2009-01-21T00:14:01.197 回答
1

有没有 java.net.URL 的 J2ME 实现?它是连接设备配置基础配置文件个人基本配置文件个人配置文件的一部分......

编辑:记录在案,这些是 CDC 1.1.2 链接,但根据JSR36,CDC 1.0 也有一个 java.net.URL 类。

于 2009-01-21T16:42:49.593 回答
1

另外,请注意,url 参数是 URL 编码的,所以你可能需要先解码它们(如何做是另一个问题)

我以这种方式获取参数:


public String getUrlParam(String url, String param)
{
    int startIndex = url.indexOf(""+param+"=");
    if (startIndex == -1)
        return null;
    int length = (""+param+"=").length();
    int endIndex = url.indexOf("&", startIndex+length);
    if (endIndex == -1)
        endIndex = url.length();
    return URLDecode(url.substring(startIndex+length, endIndex));
}

于 2009-08-17T14:45:24.407 回答
-1

URL 编码器/解码器非常简单且易于编写。您还可以在 Internet 上查找任何开源 HTML 到 WML 转码器代码并对其进行修改。应该不会太难。

于 2009-01-20T23:09:38.033 回答