gpt4 book ai didi

java - 循环调用Google Maps API对几十个地址进行地理编码并获取 "java.lang.String cannot be cast to net.sf.json.JSONObject"

转载 作者:行者123 更新时间:2023-12-01 05:41:58 25 4
gpt4 key购买 nike

下面的代码是我用来调用 Google Maps API 来获取原始地址输入的地理编码地址的代码。

如果我一次对少数地址执行此操作,则效果很好。

但是,如果我在多个地址上循环运行它,我偶尔会意外地收到此错误:

java.lang.String cannot be cast to net.sf.json.JSONObject

每次运行循环时,它都会发生在不同的输入字符串上 - 因此它似乎与特定的输入数据无关。

我可能预计会在 Google Maps API 中遇到速率限制,但此错误似乎与此无关?

或者是吗?

package com.blogspot.unserializableone;

import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import org.apache.commons.io.IOUtils;

public class GCoder {
private static final String URL = "http://maps.google.com/maps/geo?output=json";
private static final String DEFAULT_KEY = "ABQIAAAAxPbpriJFATP1kV4Jfg7FrhTHx8S3jtCwO1hw0XE3N9WOac4cqRQ8_vMKB22No44yFau5GRY7TcCHZA";

public static GAddress geocode(String address, String key) throws Exception {
URL url = new URL(URL + "&q=" + URLEncoder.encode(address, "UTF-8") + "&key=" + key);
URLConnection conn = url.openConnection();
ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
IOUtils.copy(conn.getInputStream(), output);
output.close();
GAddress gaddr = new GAddress();
JSONObject json = JSONObject.fromObject(output.toString());
JSONObject placemark = (JSONObject) query(json, "Placemark[0]");
final String commonId = "AddressDetails.Country.AdministrativeArea";
gaddr.setFullAddress(query(placemark, "address").toString());
gaddr.setZipCode(query(placemark,
commonId + ".SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber").toString());
gaddr.setAddress(query(placemark,
commonId + ".SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName")
.toString());
gaddr.setCity(query(placemark, commonId + ".SubAdministrativeArea.SubAdministrativeAreaName")
.toString());
gaddr.setState(query(placemark, commonId + ".AdministrativeAreaName").toString());
gaddr.setLat(Double.parseDouble(query(placemark, "Point.coordinates[1]").toString()));
gaddr.setLng(Double.parseDouble(query(placemark, "Point.coordinates[0]").toString()));
return gaddr;
}

public static GAddress geocode(String address) throws Exception {
return geocode(address, DEFAULT_KEY);
} /* allow query for json nested objects, ie. Placemark[0].address */

private static Object query(JSONObject jo, String query) {
try {
String[] keys = query.split("\\.");
Object r = queryHelper(jo, keys[0]);
for (int i = 1; i < keys.length; i++) {
r = queryHelper(JSONObject.fromObject(r), keys[i]);
}
return r;
} catch (JSONException e) {
return "";
}
} /* help in query array objects: Placemark[0] */

private static Object queryHelper(JSONObject jo, String query) {
int openIndex = query.indexOf('[');
int endIndex = query.indexOf(']');
if (openIndex > 0) {
String key = query.substring(0, openIndex);
int index = Integer.parseInt(query.substring(openIndex + 1, endIndex));
return jo.getJSONArray(key).get(index);
}
return jo.get(query);
}

public static void main(String[] args) throws Exception {
System.out.println(GCoder.geocode("28 275, Mercedes, Buenos Aires, Argentina"));
System.out.println(GCoder.geocode("94103"));
System.out
.println(GCoder.geocode("peña 2700 capital federal cdad. autonoma de buenos aires"));
System.out.println(GCoder
.geocode("2700 peña, capital federal, ciudad autonoma de buenos aires, argentina"));
}
}

最佳答案

看起来您的错误可能发生在这一行 --

    JSONObject placemark = (JSONObject) query(json, "Placemark[0]");

查看您的查询方法,您似乎正在吞下 JSONException 并返回一个空字符串,然后该字符串无法转换为 JSONobject。因此,这不是 Google Maps API 的异常,而是代码中的异常。我建议在该 JSONException 上打印 stackTrace 以进一步调查。

关于java - 循环调用Google Maps API对几十个地址进行地理编码并获取 "java.lang.String cannot be cast to net.sf.json.JSONObject",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6929497/

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