Android DirectionResult具有空路由和航路点-谷歌方向API
我的目标是在Android应用程序的地图视图上轮询Google Directions API并通过多段线绘制路线 但是,当我从API调用返回Android DirectionResult具有空路由和航路点-谷歌方向API,android,google-api,jackson,map-directions,google-http-client,Android,Google Api,Jackson,Map Directions,Google Http Client,我的目标是在Android应用程序的地图视图上轮询Google Directions API并通过多段线绘制路线 但是,当我从API调用返回DirectionsResult时,尝试访问DirectionsResult.routes[0]或DirectionsResult.geocodedWaypoints[0]会导致NullPointerException 我目前正在使用maven存储库导入以下库(摘自我的build.gradle): 我在AsyncTask实现中的当前代码,已注释掉调试调用:
DirectionsResult
时,尝试访问DirectionsResult.routes[0]
或DirectionsResult.geocodedWaypoints[0]
会导致NullPointerException
我目前正在使用maven存储库导入以下库(摘自我的build.gradle):
我在AsyncTask实现中的当前代码,已注释掉调试调用:
@Override
protected synchronized String doInBackground(URL... params)
{
try {
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
request.setParser(new JsonObjectParser(JSON_FACTORY));
}
});
GenericUrl url = new GenericUrl("http://maps.googleapis.com/maps/api/directions/json");
// to and from are both LatLng's
url.put("origin", from.toUrlValue());
url.put("destination", to.toUrlValue());
url.put("sensor", true);
HttpRequest request = requestFactory.buildGetRequest(url);
//wait(1000);
HttpResponse httpResponse = request.execute();
//Log.i(TAG, httpResponse.parseAsString());
//wait(1000);
DirectionsResult directionsResult = httpResponse.parseAs(DirectionsResult.class);
//wait(1000);
//Log.i(TAG, Integer.toString(httpResponse.getStatusCode()));
//Log.i(TAG, httpResponse.getStatusMessage());
latlngs = directionsResult.routes[0].overviewPolyline.decodePath();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
我的调试过程:
httpResponse.parseAsString()
时得到的响应相同
httpResponse.getStatusCode()
返回了200httpResponse.getStatusMessage()
返回OKDirectionsResult
解析JSONHttpResponse
之前,一切都正常:
DirectionsResult directionsResult = HttpResponse.parseAs(DirectionsResult.class);
之后,当访问方向结果
:路线
和地理编码航路点
的两个字段时,我会得到一个NullPointerException
有没有人在这门课上有过类似的问题?我一直在想,如果这里没有解决这个问题,我可能会在正确的google http客户端库GitHub页面上提交一个问题。因此,有两件事我做得不对 首先,我在URL中没有包含API密钥,这并不能解释为什么我能够使用
httpRequest.parseAsString()
打印格式正确的JSON响应,但不管怎样,显然需要包含一个API密钥才能对我的Google开发者帐户进行正确的计费。因此需要进行的另一个更改是将URL从“http”更改为“https”
其次,请求安卓API密钥而不是服务器API密钥似乎存在问题。我在使用Android API键时收到此响应:
{
"error_message" : "This IP, site or mobile application is not authorized to use this API key. Request received from IP address 128.210.106.49, with empty referer",
"routes" : [],
"status" : "REQUEST_DENIED"
}
作为一种解决方案,在创建API密钥时,请选择服务器选项而不是Android。Hi,当我在Android中获取从源到目标的方向API的路由时,我得到的错误是java.lang.NullPointerException。您能帮我吗?提前谢谢。
{
"error_message" : "This IP, site or mobile application is not authorized to use this API key. Request received from IP address 128.210.106.49, with empty referer",
"routes" : [],
"status" : "REQUEST_DENIED"
}