Blackberry URL encoder

懵懂的女人 提交于 2019-11-30 04:06:11

问题


I need to encode a URL using HTTP GET request in Blackberry. Can any one help me find how do I achieve this.


回答1:


Whyt don't you use RIM's URLEncodedPostData?

private String encodeUrl(String hsURL) {
    URLEncodedPostData urlEncoder = new URLEncodedPostData("UTF-8", false);
    urlEncoder.setData(hsURL);
    hsURL = urlEncoder.toString();
    return hsURL;
}



回答2:


here you go ;^)

public static String URLencode(String s)
{
    if (s!=null) {
        StringBuffer tmp = new StringBuffer();
        int i=0;
        try {
            while (true) {
                int b = (int)s.charAt(i++);
                if ((b>=0x30 && b<=0x39) || (b>=0x41 && b<=0x5A) || (b>=0x61 && b<=0x7A)) {
                    tmp.append((char)b);
                }
                else {
                    tmp.append("%");
                    if (b <= 0xf) tmp.append("0");
                    tmp.append(Integer.toHexString(b));
                }
            }
        }
        catch (Exception e) {}
        return tmp.toString();
    }
    return null;
}



回答3:


use the class provided by w3. Here is the download link




回答4:


the reply using "URLEncodedPostData" above is incorrect. Corrected sample:

public static String encodeUrl(Hashtable params)
{
    URLEncodedPostData urlEncoder = new URLEncodedPostData("UTF-8", false);
    Enumeration keys = params.keys();
    while (keys.hasMoreElements()) {
        String name = (String) keys.nextElement();
        String value = (String) params.get(name);
        urlEncoder.append(name, value);
    }

    String encoded = urlEncoder.toString();
    return encoded;
}

Cheers!



来源:https://stackoverflow.com/questions/1211299/blackberry-url-encoder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!