问题
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