【问题标题】:Java: Illegal character in URIJava:URI 中的非法字符
【发布时间】:2012-05-16 07:55:47
【问题描述】:

我试图用这个源代码请求 googles geo api

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, Berlin&sensor=false

抛出异常:非法字符!

我怎样才能逃避 ä,ü,ö,ß 和空白? 我尝试使用 ISO-8859-1 作为编码的 java.net.URLEncoder 没有成功:(

问候伊戈尔

【问题讨论】:

    标签: java apache http url uri


    【解决方案1】:

    K%C3%B6nigstra%C3%9Fe UTF-8 百分比编码也可以。

    【讨论】:

    • URLEncoder.encode("Königstraße, Berlin", "UTF-8") 产生 K%C3%B6nigstra%C3%9Fe。谢谢
    【解决方案2】:

    您需要使用 UTF-8 对单个请求参数值进行 URL 编码,而不是整个 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";
    

    【讨论】:

      猜你喜欢
      • 2011-03-15
      • 2013-03-03
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 2019-08-30
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多