0

I am working with special character in URL for android

However, I encounter two problem

1) empty space

If I have a query inside url

e.g. test.php?test=aaa bbbb cccc

Then the query will not include bbbb and cccc, I learnt that I should replace the " " to %20, however, instead of using replace(" ","%20"), how can I do it in more standard way?

2) traditional chinese in url

I have an image url like this:

http://oshc.zizsoft.com/wp-content/uploads/2014/04/1-職安健資訊產品目錄2013-220x300.png

If I directly pass to android, it fail. but if I copy the link to my desktop browser , it change to like this, then I paste it on android, it works

"http://oshc.zizsoft.com/wp-content/uploads/2014/04/1-%E8%81%B7%E5%AE%89%E5%81%A5%E8%B3%87%E8%A8%8A%E7%94%A2%E5%93%81%E7%9B%AE%E9%8C%842013-220x300.png";

What should I do to encode to this?

Thanks for helping

Update:

it change to

http%3A%2F%2Foshc.zizsoft.com%2Fwp-content%2Fuploads%2F2013%2F12%2FSQ1-351x300.jpg

How can I fix that? Thanks

user3538235
  • 1,991
  • 6
  • 27
  • 55

2 Answers2

1

1) Try trimming the url, it wil remove the spaces from url. I guess its bit more staanddard way to solve this issue.

2) This problem is eactly due to the encoding issues. Our Android default encoding is cp1252...and those strings will not be encoded in the same way so when it used in the code its automatically changes to some other symbols. So try changing the encoding of the project and string to the same like UTF-8 or something. You can change project encoding by Properties> Resources> Encoding

Hope my suggesions will help you a bit.

Try this links also 1. Trimming 2. Encoding

Community
  • 1
  • 1
Sreedev
  • 6,563
  • 5
  • 43
  • 66
1

If you want to programmatically encode and decode URLs then use URLEncoder and URLDecoder class available in Java as well as Android.

// To encode URL 
URLEncoder.encode(url, charset);
// to decode url
URLDecoder.decode(url, charset);
aksdch11
  • 683
  • 6
  • 14