Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android DirectionResult具有空路由和航路点-谷歌方向API_Android_Google Api_Jackson_Map Directions_Google Http Client - Fatal编程技术网

Android DirectionResult具有空路由和航路点-谷歌方向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实现中的当前代码,已注释掉调试调用:

我的目标是在Android应用程序的地图视图上轮询Google Directions API并通过多段线绘制路线

但是,当我从API调用返回
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;
}
我的调试过程:
  • 这是我打印的请求url:

  • 显然,这是一个简短但完整的通话。腿的元素包含多段线点(这是我假设的目标)。这个JSON与我在阅读
    httpResponse.parseAsString()
    时得到的响应相同

  • 在查看一些StackExchange问题时,我看到一些人建议等待接收数据。我在请求周期的所有组成部分之间设置了1秒的延迟,但没有结果

  • 调用的其他部分均为空

  • httpResponse.getStatusCode()
    返回了200

  • httpResponse.getStatusMessage()
    返回OK

  • 在我尝试使用
    DirectionsResult
    解析JSON
    HttpResponse
    之前,一切都正常:

    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"
    }