2012-05-07 72 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