【发布时间】:2015-07-24 10:26:37
【问题描述】:
我需要对地址进行地理编码并获取城市的纬度、经度、邮政编码等。
当我将此请求与 Android 一起使用时 https://maps.googleapis.com/maps/api/geocode/json?address=Aachen-Horbach+Gzg®ion=DEU&sensor=false
我收到 INVALID_REQUEST 作为响应。但是当我在浏览器中或借助 REST 客户端尝试此链接时,一切正常。
这个请求也一样:
https://maps.googleapis.com/maps/api/geocode/json?address=Abertamy-Horní+Blatná®ion=CZE&sensor=false
可能是什么原因?
网址创建:
StringBuilder addressUrl = new StringBuilder();
if (order.getDepartureAddress().getStreet()!=null && !order.getDepartureAddress().getStreet().equalsIgnoreCase(""))
addressUrl.append(order.getDepartureAddress().getStreet() + ", ");
if (order.getDepartureAddress().getHouseNumber()!=null && ! order.getDepartureAddress().getHouseNumber().equalsIgnoreCase(""))
addressUrl.append(order.getDepartureAddress().getHouseNumber() + ", ");
if (order.getDepartureAddress().getCity()!=null && !order.getDepartureAddress().getCity().equalsIgnoreCase(""))
addressUrl.append(order.getDepartureAddress().getCity() );
if (order.getDepartureAddress().getCountryCode()!=null && !order.getDepartureAddress().getCountryCode().equalsIgnoreCase(""))
addressUrl.append("®ion="+order.getDepartureAddress().getCountryCode() );
addressUrl.append("&sensor=false");
/*
Constants.URL_GEOCODING = https://maps.googleapis.com/maps/api/geocode/json?address=
*/
String finalUrl = Constants.URL_GEOCODING + addressUrl.toString();
/* request execution : */
public static JSONObject readJsonFromUrl(String urlStr) throws IOException, JSONException {
URL url = new URL(urlStr);
URI uri = null;
URI uri2= null;
try {
uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();
uri2 = new URI(url.toString().replace("%20", "+"));
} catch (URISyntaxException e) {
e.printStackTrace();
}
url = uri2.toURL();
Log.i(TAG + " requested url", url.toString());
InputStream is = url.openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}
【问题讨论】:
-
我喜欢你的名字。
Rikki Tikki Tikki Tavi -
也许你有编码问题。请添加你称之为
url的android代码 -
将其添加到我的问题中
标签: java android google-maps google-maps-api-3