1

All I want to do is to send a URL String into my RESTFUL web service with some kind of code like this

URL someURL= new URL("http://myWebService:port/service/"+CharSequence.getText());

Its all going well until I found error with space character in my URL. I found some solution about replacing the space character with %20 which is I already defined with something like this :

URL someURL= new URL("http://myWebService:port/service/"+CharSequence.getText().replace(" ", "%20"));

Everything, again, seems going well until i found that the replace(Char oldChara, Char newChara) function can only replace ONE space character, and not two.

For brief example when I send the CharSequence.getText() with values "We won" there will be no error, but when I change the values into "We won the battles" there will be an error issuing that there are some illegal character sent to my RESTFUL web service.

Any kind of answer will come up with my great thanks and big salute ~Regards~

1 Answer 1

1

Use replaceAll instead of replace.
Although, you should really be doing proper URL encoding. You can use URLEncoder.encode
for example.

Sign up to request clarification or add additional context in comments.

6 Comments

What is WMD anyway? Sorry Im a newbie in web programming IMHO, the replaceAll isn't reversible. I cant read back what I've already send to the web service since the values is already changed drastically (the space character is long gone already)
@Lebriga - ignore the WMD comment. it's the SO text editor Nd it was giving me some trouble with the links.
If you want to use replaceAll, you should just use it instead of replace in the sample code in the question. however, there are other characters besides spaces that are not allowed in URLs, hence why you need to use URLEncoder class to properly replace all of them.
well could you give me some example of usage in URL Encoder? i dont know the character format that i should converted to. Moreover i dont really understand about character formatting (UTF-8, etc). Lets say the annoying character that i want to get rid is space character only, could you give the example? ~Regards~ ~English is my 2nd language~
you should specify UTF-8 as the second parameter (the encoding). Here's an example how to use that class: devdaily.com/blog/post/java/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.