2012-05-07 49 views
1

我试图请求Google的地理API与此源代码爪哇:在URI非法字符

client = new DefaultHttpClient(); 
    HttpGet get=new HttpGet(uri); 
     try { 
      HttpResponse response = client.execute(get); 
      int statusCode = response.getStatusLine().getStatusCode(); 
      if (statusCode == 200){ 
        HttpEntity entity = response.getEntity(); 
        InputStream is = entity.getContent(); 
        try { 
         XMLReader parser = XMLReaderFactory.createXMLReader(); 
         parser.setContentHandler(gh); 
         parser.parse(new InputSource(is)); 
        } catch (IllegalStateException e) { 
         e.printStackTrace(); 
        } catch (SAXException e) { 
         e.printStackTrace(); 
        } 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

是URI这样 http://maps.googleapis.com:80/maps/api/geocode/xml?address =的Königstraße,柏林&传感器=假

,则抛出异常:非法角色!

我怎样才能逃脱ä,ü,ö,ß和空白? 我试图与ISO-8859-1的java.net.URLEncoder中如没有成功:(

问候伊戈尔

回答

5

编码,你需要URL编码与UTF-8,而不是整个的invididual请求参数值URL,也不能与ISO-8859-1。

String url = "http://maps.googleapis.com:80/maps/api/geocode/xml" 
    + "?address=" + URLEncoder.encode("Königstraße, Berlin", "UTF-8") 
    + "&sensor=false"; 
+0

你说得对,是这样工作的。谢谢你这么多:) – Igor

0

ķ%C3%B6nigstra%C3%9FE UTF-8编码的百分比将正常工作。

+0

URLEncoder.encode(“Königstraße,Berlin”,“UTF-8”)产生K%C3%B 6nigstra%C3%9FE。谢谢 – Igor