Tex*_*Tex 3 java api google-maps http jsoup
我正在开展一个项目,要求我在谷歌地图上找到一些商店的坐标.我已经有了每个商店的地址.
我使用了Google地理编码API,我认为它们是我需要的:我所需要做的就是连接到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();
}
}
Run Code Online (Sandbox Code Playgroud)
}
投
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)
Run Code Online (Sandbox Code Playgroud)
试试这个
Jsoup.connect(url).ignoreContentType(true).execute().body();
Run Code Online (Sandbox Code Playgroud)
要么
Jsoup.connect(url).ignoreContentType(true).timeout(60 * 1000).userAgent(ua).get();
Run Code Online (Sandbox Code Playgroud)