【发布时间】:2013-01-12 16:54:23
【问题描述】:
我正在开展一个项目,该项目需要我在 Google 地图上查找一些商店的坐标。我已经有了每家商店的地址。
我用Google Geocoding APIs 玩了一点,我认为它们是我需要的:我的代码需要做的就是连接到 DBMS,检索 item_id 和地址,为地理编码 API 生成一个有效的 URL 并处理它会得到 JSON 数据。
我不明白为什么,但我生成的 URL 在我的浏览器(Chrome 23 和最新的 Safari、OS X)中有效,但在 Jsoup 中无效。我在 Chrome 中查看了页面的源代码,看起来它是完全有效的 HTML。那么 Jsoup 做错了什么?
代码 sn-p(可运行,会给你我得到的相同异常):
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,+New%20York,+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)
【问题讨论】:
-
您需要设置
Content-Type。像这样Jsoup.connect(url).header("Content-Type","application/xml")
标签: java api http google-maps jsoup