Hi, I need to encode a URL using HTTP GET request in Blackberry. Can any one help me find how do I achieve this.
+1
A:
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;
}
Toad
2009-07-31 08:54:22
+3
A:
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;
}
Max Gontar
2009-07-31 09:01:47
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.
Toad
2009-07-31 09:24:14
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.
Max Gontar
2009-07-31 09:35:21
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.
Marc Novakowski
2009-08-03 23:03:44