我有几个特殊字符 url,我必须使用 Jsoup.connect(string) 连接到它,但它无法加载页面(出现错误 500)。我对 URL 等不太感兴趣,但我认为这与 JSoup.connect 使用的编码有关
无论如何,我将如何继续以允许链接具有特殊字符,例如:
Æ Ø Å è 等,我得到的异常(exception)是:
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)
产生此错误的代码是:
Document gDoc = Jsoup.connect(placesURL).get();
其中placesURL 字符串是:
https://maps.googleapis.com/maps/api/place/textsearch/xml?query=%s&sensor=false&key=XX&radius=10
任何人都知道解决这个问题吗?
谢谢!
最佳答案
遇到 URL 编码问题,我建议您首先使用 URL 编码器工具 ( StackOverflow answer regarding those ) 解析您的请求。一个已经带有Java。
URLEncoder.encode(stringToBeEncoded, "UTF-8")
在上面的未格式化字符串上使用它,它应该看起来像:
Document gDoc = JSoup.connect(placesURL.format(URLEncoder.encode(queryString, "UTF-8"));
...至于不对您的整个 URL 进行 URL 编码,只是您需要符合 UTF-8(或 UTF-16)的查询的一部分。
关于Jsoup.connect(string) 编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12459590/