Jsoup.connect(string)编码问题 [英] Jsoup.connect(string) encoding issues

查看:418
本文介绍了Jsoup.connect(string)编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须通过Jsoup.connect(string)连接到几个特殊字符的url,但是它无法加载页面(错误500).我对URL的了解并不多,但我认为它与JSoup.connect

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)

产生此错误的代码是:

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

placesURL字符串在哪里:

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?

谢谢!

推荐答案

遇到URL编码问题,建议您首先使用URL编码器工具(

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"));

...关于不对整个URL进行URL编码,只是查询中需要符合UTF-8(或UTF-16)要求的部分.

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

这篇关于Jsoup.connect(string)编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆