Blackberry URL encoder
Asked Answered
V

4

8

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

Vaniavanilla answered 31/7, 2009 at 8:23 Comment(0)
A
18

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;
}
Accentor answered 31/7, 2009 at 9:1 Comment(5)
Great solution but not portable. Given he wants to run his software on a different mobile, he'll be asking the same question again. Best is to steer clear of classes which only run on one platform.Quintin
Can't tell for sure... In this case you're right, cause it's not platform dependent functionality. But still simple is good, implement it when they ask you.Accentor
He doesn't actually say he's writing cross-platform mobile code so in this case I'd side with coldice - it seems safer to me (less likely to introduce bugs) to use a native API over a homebrew approach.Egwin
Maybe I'm misunderstanding the question here, but I don't think this answer does what the question asks, at all. URLEncodedPostData is meant for URL encoding sets of POST params (key/value pairs), to be written as content bytes in a POST request. It looks to me (and to a couple other answers here) that the OP is asking to encode the URL itself. For example, http://maps.google.com/?addr=123 Main St, New York, NY -> http://maps.google.com/?addr=123+Main+St,+New+York,+NY. This doesn't do that.Finnie
I am not able to get a proper URL encoded string from this solution : here is my link maps.google.com/maps?saddr=HD5&DT4 8TATerryterrye
Q
8

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;
}
Quintin answered 31/7, 2009 at 8:54 Comment(0)
U
6

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

Unzip answered 2/12, 2009 at 21:15 Comment(0)
W
3

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!

Warmongering answered 17/3, 2012 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.