繁体   English   中英

使用Google Maps API REQUEST_DENIED

[英]REQUEST_DENIED using Google Maps API

我正在使用以下方法使用Apache HTTP客户端调用Google Maps API。

  protected synchronized String submitUrl(String url) throws AjApiException {
    //split off the parameters for the post
    String uri = extractUri(url);
    HttpPost httpPost = new HttpPost(uri);
    httpPost.setEntity(buildHttpEntityParmsFromUrl(url));
    String json = null;
    try {
      HttpResponse response = httpClient.execute(httpPost);
      HttpEntity entity = response.getEntity();
      //should be JSON
      Header header = entity.getContentType();
      log.debug("Content type:" + header.getValue());
      //read the body
      json = readBody(entity.getContent());
      // Read any remaining portions of the entity so we don't keep the data around
      EntityUtils.consume(entity);
    }
    catch (ClientProtocolException cpe) {
      String msg = "Error using:" + url;
      log.error(msg, cpe);
      throw new AjApiException(msg, cpe);
    }
    catch (IOException ioe) {
      String msg = "Error using:" + url;
      log.error(msg, ioe);
      throw new AjApiException(msg, ioe);
    }
    finally {
      httpPost.releaseConnection();
    }
    log.debug("Results:" + json);

    if(json.startsWith(KeyNames.ERROR_INDICATOR)){
      List<String> parts = TranslationHelper.parseCsvLine(json.trim());
      String msg = parts.get(1);
      int errRsn;
      try {
        errRsn = Integer.parseInt(parts.get(2));
      }
      catch (NumberFormatException nfe) {
        errRsn = KeyNames.ERRRSN_UNDEFINED;
      }
      throw new AjApiException(msg, errRsn);
    }
    else{
      return json;
    }
  }


  /**
   * From a URL, extract everything up to the parameters
   * @param url the url
   * @return the resource path from the URL
   */
  public static final String extractUri(String url){
    String result = null;
    int pos = url.indexOf("?");
    if(pos>=0){
      //strip the parms
      result = url.substring(0, pos);
    }
    else{
      //no parms, just return
      result = url;
    }

    return result;
  }

  /**
   * Remove the parameters coded on the URL and create an
   * HttpParams object with those parameters
   * @param url the URL
   * @return the http parameters
   */
  public static UrlEncodedFormEntity buildHttpEntityParmsFromUrl(String url){
    List<NameValuePair> formparams = new ArrayList<NameValuePair>();

    int pos = url.indexOf("?");
    if(pos>=0){
      //process parms
      String queryString = url.substring(pos + 1).trim();
      if(queryString.length() > 0){
        String[] queryParms = queryString.split("&");
        for(String queryParm:queryParms){
          String[] parts = queryParm.split("=");
          formparams.add(new BasicNameValuePair(parts[0], parts[1]));
        }
      }
    }

    UrlEncodedFormEntity result;
    try {
      result = new UrlEncodedFormEntity(formparams);
    }
    catch (UnsupportedEncodingException uee) {
      throw new IllegalStateException(uee);
    }

    return result;
  }

我传递的网址是: http : //maps.googleapis.com/maps/api/directions/json?sensor=false&origin=PO+BX+4471,TUSTIN,CA,92782&destination=700+Civic+Center+Drive + West +-+ 3rd + Floor,Santa + Ana,CA-California,92702 ,它调用了Google Maps API。

当我在浏览器中键入此URL时,我将以JSON的方式获取路由信息。 当使用上述方法调用它时,我得到:

{
   "routes" : [],
   "status" : "REQUEST_DENIED"
}

在这一点上,我不确定要去哪里。

问题是由于我的软件使用的是HTTP Post,而Google只接受get。 我更改为致电以获取并解决了该问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM