Jsoup.connect(string) encoding issues

前端 未结 2 1111
我寻月下人不归
我寻月下人不归 2021-01-18 11:05

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

相关标签:
2条回答
  • 2021-01-18 11:16

    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!

    0 讨论(0)
  • 2021-01-18 11:27

    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.

    0 讨论(0)
提交回复
热议问题