Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 170
Registered: ‎01-17-2009
My Device: 9630
Accepted Solution

URL Encoding and Decoding

Hi,

 

I need a way to encode and decode URL's but J2ME doesn't include anything that makes this easy.

 

Has anybody found a solution?

Greg 

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: URL Encoding and Decoding

You can encode the URL data using URLEncodedPostData in the RIM API.

 

Developer
Posts: 5,339
Registered: ‎09-20-2008
My Device: ***
My Carrier: ***

Re: URL Encoding and Decoding

New Developer
Posts: 3
Registered: ‎01-26-2009
My Device: Not Specified

Re: URL Encoding and Decoding

OK that array in that example seems a little much.  The %   encoding is very simple it is a % followed by the 2 digit hex representation of the byte.  So you wouldn't need the array, you would just use:

 

int i = Integer.parseInt(s);

string hex = Integer.toHexString(i);

if ( hex.length() < 2 )

    hex = "0" + hex;

 

hex = "%" + hex;

 

You would only really need to do this for characters that fall into the brach of being invalid URL characters http://www.w3schools.com/tags/ref_urlencode.asp.

 

When I did this a long time ago I did everything outside the a-zA-Z range so I didn't have to do a lot of testing of values.

Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: URL Encoding and Decoding

[ Edited ]

That's not going to work for characters outside the range 0x00-0xFF. (For that matter, neither does URLEncodedPostData.) For more general data handling (say, the Euro currency symbol, non-Latin alphabets, etc.), you need something a lot better. According to RFC 3986, the following transformations are required:

 

characters => UTF-8 octet stream => URI-encoded characters

 

Decoding does these transformations in the reverse.

 

The code at w3.org is rather old (RFC 3986 was published in 2005) and isn't exactly correct. For one thing, it assumes that Unicode ends at U+FFFF, which isn't the case these days. It also fails to encode certain characters that are now reserved as subcomponent delimiters by RFC 3986. I recently posted some code in this thread that does the job. It isn't anywhere near as efficient as the W3 code--but as the saying goes, "I'd rather optimize debugged code than debug optimized code.

Message Edited by Ted_Hopp on 04-02-2009 10:26 PM



Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
New Developer
Posts: 3
Registered: ‎02-03-2010
My Device: bold 9700

Re: URL Encoding and Decoding

Hi guys,

I know this is not the answer, but this tool can help

This is a url encode online tool

Pretty useful

David

Developer
Posts: 137
Registered: ‎09-30-2009
My Device: Torch 9800

Re: URL Encoding and Decoding

check this link

http://dev.telnic.org/trac/browser/apps/blackberry/trunk/blackberry/src/org/not/java/net/URLDecoder....

----------------------
Press like if you find a message helpful
---------
Developer
Posts: 26
Registered: ‎07-23-2008
My Device: Not Specified

Re: URL Encoding and Decoding

The post that's marked as the solution doesn't handle UTF-8 characters correctly when decoding. raquibulbari's post does so that should be marked as the solution.