JAram
JAram

Reputation: 3

Special characters in URL java not working

I'm making a program that gets data from league of legends api, it analizes your current game and shows you your oponents names, ranks and stuff. But i have an issue because to get someone's rank i need to travel to a specific url with his nick in it. The problem is that some players put characters like å, à, è, ö, or stuff like that in their nicks, and when that happens my program stops working. I will show you part of the code that im using to do that:

getSummonerID2 = new JSONObject(IOUtils.toString(newURL("https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by-name/"+ newPlayerNick[i] + "?api_key=" + APIKEY), Charset.forName("UTF-8")));

This is the error it throws: java.io.IOException: Server returned HTTP response code: 400 for URL: https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by-name/JegFårDetDårligt?api_key=( My key, cant show it sry)

It happened to me before with the spaces, because it looks like you cant even put spaces in a url, so the thing i did to solve that was:

newPlayerNick[i] = playerNick[i].replaceAll("\\s+","_");

That basically replaces every space with a _, so it works perfectly, but i cant replace an "å" with a normal "a" as they are different characters.

So, do you know any solution for this? thank you so much guys.

Upvotes: 0

Views: 2603

Answers (1)

Hashcon
Hashcon

Reputation: 597

You can try URL encoding for special characters:

URLEncoder.encode("This string has å à è ö and spaces","UTF-8")

the output is like:

This+string+has+%C3%A5+%C3%A0+%C3%A8+%C3%B6+and+spaces

You can try this url to see if it works:

https://www.google.com.hk/search?q=This+string+has+%C3%A5+%C3%A0+%C3%A8+%C3%B6+and+spaces

you can see it is an google link and the keyword is passed

Upvotes: 2

Related Questions