0

I can not find an answer to that and I do not know how to compile org.apache.commons.lang.StringEscapeUtils, which is the only solution I found on the internet.

Also, I do not understand why I can not do something like this

s = s.replaceAll(" ","\\ ");
Karthikeyan
  • 7,700
  • 10
  • 46
  • 66
Greyshack
  • 1,803
  • 6
  • 24
  • 45
  • What are you trying to do? – Bojan Kseneman Apr 28 '15 at 21:00
  • I have a string in which I want to escape all characters that need to be escaped in order for the string to be considered a proper query for an url. – Greyshack Apr 28 '15 at 21:01
  • 1
    Well adding a backslash certainly doesn't perform URL escaping. It's not like "escaping" is one specification, universally applicable - there are different types of escaping for different contexts. You want URL escaping. I suspect http://stackoverflow.com/questions/3286067 is what you want. – Jon Skeet Apr 28 '15 at 21:03
  • 1
    Then don't use replace all. Use url encode – Bojan Kseneman Apr 28 '15 at 21:04
  • Thanks Jon, that is exactly what I looked for. Didn't know there is such a thing as URLEncoder. You can post the answer and I'll accept it. – Greyshack Apr 28 '15 at 21:07
  • It would be sad if a programming language like Java would not have something like that. :)) – Bojan Kseneman Apr 28 '15 at 21:09
  • Use java.net.URLEncoder: java.net.URLEncoder.encode(queryStr, "UTF-8"); – Jay Apr 28 '15 at 21:19

2 Answers2

2

I think you can use

URLEncoder.encode("your URL here", "UTF8")

Jayesh Elamgodil
  • 1,437
  • 11
  • 15
0

Use this:

String urlStr = "http://abc.dev.domain.com/0007AC/ads/800x480 15sec h.264.mp4";
URL url = new URL(urlStr);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();

Source: Url encoding in Android
This should be the correct answer in that post.

Community
  • 1
  • 1
MiguelHincapieC
  • 5,297
  • 5
  • 39
  • 66