gpt4 book ai didi

java - Jsoup 在 Google Maps API 上返回错误 400

转载 作者:可可西里 更新时间:2023-11-01 16:05:20 25 4
gpt4 key购买 nike

我正在做一个项目,需要我在 Google map 上找到一些商店的坐标。我已经有了每个商店的地址。

我玩了一点 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,+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)

最佳答案

试试这个

Jsoup.connect(url).ignoreContentType(true).execute().body();

Jsoup.connect(url).ignoreContentType(true).timeout(60 * 1000).userAgent(ua).get();

关于java - Jsoup 在 Google Maps API 上返回错误 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14577501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com