java – Jsoup在Google Maps API上返回错误400
发布时间:2020-12-15 08:50:01 所属栏目:Java 来源:网络整理
导读:我正在开展一个项目,要求我在谷歌地图上找到一些商店的坐标.我已经有了每个商店的地址. 我在Google Geocoding APIs上玩了一点,我认为它们是我需要的:我所需要做的就是连接到DBMS,检索item_id和地址,生成地理编码API的有效URL并处理它将获得的JSON数据. 我不
我正在开展一个项目,要求我在谷歌地图上找到一些商店的坐标.我已经有了每个商店的地址.
我在Google Geocoding APIs上玩了一点,我认为它们是我需要的:我所需要做的就是连接到DBMS,检索item_id和地址,生成地理编码API的有效URL并处理它将获得的JSON数据. 我不明白为什么,但我生成的URL在我的浏览器中运行(Chrome 23和最新的Safari,OS X),但在Jsoup中不起作用.我查看了Chrome中页面的来源,看起来它是完全有效的HTML.那么什么是Jsoup做错了? 代码片段(可运行,将为您提供相同的异常): import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; public class RandomClass { public static void main(String args[]) { Document doc = null; try { String url = "http://maps.googleapis.com/maps/api/geocode/json?address=0+164+W+75th+St,+New%20York,+NY+10024&sensor=false"; String ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.17 (KHTML,like Gecko) Chrome/24.0.1312.56 Safari/537.17"; doc = Jsoup.connect(url).timeout(60 * 1000).userAgent(ua).get(); } catch (IOException e) { e.printStackTrace(); } } } 投 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*,application/xml,or application/xhtml+xml. Mimetype=application/json; charset=UTF-8,URL=http://maps.googleapis.com/maps/api/geocode/json?address=164+W+75th+St,+NY+10024&sensor=false at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:436) at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:393) at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:159) at org.jsoup.helper.HttpConnection.get(HttpConnection.java:148) at asdru.RandomClass.main(RandomClass.java:16) 解决方法
试试这个
Jsoup.connect(url).ignoreContentType(true).execute().body(); 要么 Jsoup.connect(url).ignoreContentType(true).timeout(60 * 1000).userAgent(ua).get(); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |