Problems with Jsoup.connect (string) encoding

I have some special url characters with which I have to connect with Jsoup.connect (string), but it cannot load the page (getting error 500). I am not so strong in the URL and such, but I think this has something to do with the encoding used by JSoup.connect

In any case, how can I continue so that the links have special characters: Æ Ø Å è, etc. Exception I get:

java.io.IOException: 500 error loading URL https://maps.googleapis.com/maps/api/place/textsearch/xml?query=Averøy%20restaurant%20og%20Pizzeria,%20Norge&sensor=false&key=xx&radius=10 at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:414) at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:391) at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:157) at org.jsoup.helper.HttpConnection.get(HttpConnection.java:146) at HTMLParser.doParsing(HTMLParser.java:122) at HTMLParser.initParser(HTMLParser.java:50) at Main.main(Main.java:15) 

Code generating this error:

 Document gDoc = Jsoup.connect(placesURL).get(); 

Where is the placesURL line:

 https://maps.googleapis.com/maps/api/place/textsearch/xml?query=%s&sensor=false&key=XX&radius=10 

Anyone have an idea to get around this?

Thanks!

+6
source share
2 answers

If you run into problems with URL encoding, I would recommend that you first analyze your request using the URL encoder tool ( fooobar.com/questions/925575 / ... ). Already have Java.

 URLEncoder.encode(stringToBeEncoded, "UTF-8") 

Using it in its unformatted line above, it should look something like this:

 Document gDoc = JSoup.connect(placesURL.format(URLEncoder.encode(queryString, "UTF-8")); 

... in order not to URL-encode the entire URL, only part of the request must be compatible with UTF-8 (or UTF-16).

+6
source

How stupid of me, Instead of just encoding the query string, I encoded the entire URL.

Having solved this:

 String placesUrl = String.format("https://maps.googleapis.com/maps/api/place/textsearch/xml?query=%s&sensor=false&key=XX&radius=10",URLEncoder.encode(restaurantListe[i][0],"UTF-8")); 

Thanks for the help!

+4
source

Source: https://habr.com/ru/post/925572/


All Articles