|
This topic has
4
replies
on
1
page.
|
|
|
|
|
|
|
|
|
Is there any way to url encode a string in J2ME?
Thanks,
Wallace
|
|
shmoove
Posts:1,442
Registered: 9/26/02
|
|
|
|
|
Yeah, manually. There are no built-in methods but making one is a piece of cake.
shmoove
|
|
scuba
Posts:2
Registered: 5/17/05
|
|
|
|
|
That's not a very helpful answer. If its that easy, why don't you tell us how its done ?
|
|
|
|
|
|
|
|
Here is the sample code
/**
* Encode a string according to W3C standards.
*
*/
public static String urlEncoder(String s) {
if (s == null)
return s;
StringBuffer sb = new StringBuffer(s.length() * 3);
try {
char c;
for (int i = 0; i < s.length(); i++) {
c = s.charAt(i);
if (c == '&') {
sb.append("&");
} else if (c == ' ') {
sb.append('+');
} else if (
(c >= ',' && c <= ';')
|| (c >= 'A' && c <= 'Z')
|| (c >= 'a' && c <= 'z')
|| c == '_'
|| c == '?') {
sb.append(c);
} else {
sb.append('%');
if (c > 15) { // is it a non-control char, ie. >x0F so 2 chars
sb.append(Integer.toHexString((int)c)); // just add % and the string
} else {
sb.append("0" + Integer.toHexString((int)c));
// otherwise need to add a leading 0
}
}
}
} catch (Exception ex) {
return (null);
}
return (sb.toString());
}
|
|
scuba
Posts:2
Registered: 5/17/05
|
|
|
|
|
Thank you Manas. That's fixed my problem and saved me a lot of time. I'll also learn how it works!
|
|
|
This topic has
4
replies
on
1
page.
|
|
Back to Forum
Read the Developer Forums Code of Conduct
Email this Topic
Edit this Topic
|
|
Site Upgrade
Forums 7.1.8 was deployed Oct 26th. The release consists of minor fixes & a few enhancements.
|
|
Forums Statistics
Users Online : 27 - Guests : 138
About Sun forums
-
Sun Forums is a large collection of user generated
discussions. It is here to help you ask questions, find answers, and
participate in discussions.
Check out our guide on Getting
started with Sun Forums for a full walkthrough of how to best
leverage the benefits of this community.
|
|