I need to encode a URL using HTTP GET request in Blackberry. Can any one help me find how do I achieve this.
Blackberry URL encoder
Asked Answered
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;
}
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 8TA –
Terryterrye
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;
}
use the class provided by w3. Here is the download link
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!
© 2022 - 2024 — McMap. All rights reserved.