participate


Sun Java Wireless Toolkit - URL encode a string??
<<   Back to Forum  |   Give us Feedback
This topic has 4 replies on 1 page.
wallaceoc
Posts:50
Registered: 2/7/03
URL encode a string??   
Feb 7, 2005 1:57 PM

 
Is there any way to url encode a string in J2ME?

Thanks,

Wallace
 
shmoove
Posts:1,442
Registered: 9/26/02
Re: URL encode a string??   
Feb 8, 2005 12:36 AM (reply 1 of 4)  (In reply to original post )

 
Yeah, manually. There are no built-in methods but making one is a piece of cake.

shmoove
 
scuba
Posts:2
Registered: 5/17/05
Re: URL encode a string??   
May 17, 2005 9:38 AM (reply 2 of 4)  (In reply to #1 )

 
That's not a very helpful answer. If its that easy, why don't you tell us how its done ?
 
Manas@Mchex
Posts:76
Registered: 2/26/04
Re: URL encode a string??   
May 17, 2005 9:54 AM (reply 3 of 4)  (In reply to #2 )

 
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("&amp;");
				} 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
Re: URL encode a string??   
May 18, 2005 1:34 AM (reply 4 of 4)  (In reply to #3 )

 
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

Click to email this message Email this Topic

Edit this Topic
  
 
 
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.

Powered by Jive Forums