2013-04-14 94 views
0

我想打电话给谷歌距离矩阵API,但在这里115得到一个错误是非法的查询是我的代码:错误在查询谷歌距离矩阵API

protected Document doInBackground(LatLng... latlng) { 

     String destinationURL = ""; 
     // ASSUMING THAT FIRST LATLNG PASSED IS ALWAYS A SOURCE LOCATION 
     for(int index = 1; index < latlng.length; index ++) 
     { 
      destinationURL += latlng[index].latitude +"," + latlng[index].longitude; 
      if(index+1 != latlng.length) 
      { 
       destinationURL+= "|"; 
      } 
     } 
     String url = "http://maps.googleapis.com/maps/api/distancematrix/xml?" 
       + "origins=" + latlng[0].latitude + "," + latlng[0].longitude 
       + "&destinations=" + destinationURL 
       + "&sensor=false&mode=walking"; 

如果我粘贴上面则给出的网址这是结果:URL link of query

错误的详细信息:java.lang.IllegalArgumentException: Illegal character in query at index 115: http://maps.googleapis.com/maps/api/distancematrix/xml?origins=35.777418,-78.677666&destinations=35.78036,-78.67816|35.787515,-78.670456&sensor=false&mode=walking

代码调用上述网址:

try { 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpContext localContext = new BasicHttpContext(); 
      HttpPost httpPost = new HttpPost(url); 
      HttpResponse response = httpClient.execute(httpPost, localContext); 
      InputStream in = response.getEntity().getContent(); 
      DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
      Document doc = builder.parse(in); 
      return doc; 

任何帮助v.much赞赏。

+1

把它扔进浏览器的作品。 | |需要编码到%7C? –

+0

我不确定,我在android应用程序中使用它,它崩溃在:HttpPost httpPost = new HttpPost(url);.完全卡住了 – Rohit

+0

你是对的@加布塞尚。我只注意到“|”管道字符在URLencoder.encode(|)后转换为%7C。当你说如果|时我没有第一次需要编码到%7C中,但现在可以理解了 – Rohit

回答

3

经过几次尝试后发现此问题。我正在使用管道字符“|”加入经纬度。注意管道字符只是一个字符串。 但是为了在网址中使用添加竖线URLEncoder的最终的字符串添加:

destinationURL += latlng[index].latitude +"," + latlng[index].longitude; 
if(index+1 != latlng.length) 
    { 
     try { 
      destinationURL+= URLEncoder.encode("|", "UTF-8"); 
      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

它成功合作。