6

I have several special character url's i have to connect to with Jsoup.connect(string), but it fails to load the page (getting a error 500). I'm not really that much into URL's and such but i think it has somthing to do with the encoding used by JSoup.connect

Anyways, how would i proceed in order to allow links to have special characters like: Æ Ø Å è etc, The exception i'm getting is:

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)

The code which is producing this error is:

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

Where the placesURL string is:

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

Anyone have any idea to get around this?

Thanks!

user1677631
  • 103
  • 1
  • 5

2 Answers2

6

Running into URL-encoding problems, I'd recommend you parse your request using a URL encoder tool first (StackOverflow answer regarding those). One already comes with Java.

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

Using it on your unformatted string above, it should look something like:

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

... as to not URL-encode your entire URL, just the part of the query you need to be UTF-8 (or UTF-16) compliant.

Community
  • 1
  • 1
  • 1
    %s is the string which the query is processing (via String.Format) and i already got a key, Using URLEncoder.encode(stringToBeEncoded, "UTF-8"), gave me a Malformed URL error; java.lang.IllegalArgumentException: Malformed URL: https%3A%2F%2Fmaps.googleapis.com%2Fmaps%2Fapi%2Fplace%2Ftextsearch%2Fxml%3Fquery%3D1001%2520Natt%2C%2520Norge%26sensor%3Dfalse%26key%lkg%26radius%3D10 – user1677631 Sep 18 '12 at 11:41
  • In this case you will only use the URLEncoder on the "query" part of your string... e.g. JSoup.connect(placesURL.format(URLEncoder.encode(queryString, "UTF-8")); –  Sep 18 '12 at 13:10
4

How stupid of me, Instead of just encoding the query string, i encoded the whole URL..

Solved by doing 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!

user1677631
  • 103
  • 1
  • 5
  • 1
    I'm no longer certain who deserves "best answer" first, lol. Glad I could help, though. –  Sep 18 '12 at 17:02