我有几个特殊的字符url我必须连接到Jsoup.connect(字符串),但它无法加载页面(得到错误500).我不是那么多的URL,但我认为它与JSoup.connect使用的编码有关
无论如何,我将如何继续以允许链接具有特殊字符,如:ÆØÅè等,我得到的例外是:
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)
Run Code Online (Sandbox Code Playgroud)
产生此错误的代码是:
Document gDoc = Jsoup.connect(placesURL).get();
Run Code Online (Sandbox Code Playgroud)
placesURL字符串的位置是:
https://maps.googleapis.com/maps/api/place/textsearch/xml?query=%s&sensor=false&key=XX&radius=10
Run Code Online (Sandbox Code Playgroud)
任何人有任何想法绕过这个?
谢谢!
小智 6
遇到URL编码问题,我建议您先使用URL编码器工具解析您的请求(StackOverflow回答这些问题).一个已经附带Java.
URLEncoder.encode(stringToBeEncoded, "UTF-8")
Run Code Online (Sandbox Code Playgroud)
在上面未格式化的字符串上使用它,它应该类似于:
Document gDoc = JSoup.connect(placesURL.format(URLEncoder.encode(queryString, "UTF-8"));
Run Code Online (Sandbox Code Playgroud)
...至于不对您的整个URL进行URL编码,只需要您需要符合UTF-8(或UTF-16)的查询部分.