简体   繁体   中英

Java: Illegal character in URI

I tried to request googles geo api with this source code

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();
        }

Is the URI like this http://maps.googleapis.com:80/maps/api/geocode/xml?address =Königstraße, Berlin&sensor=false

An exception is thrown: Illegal character!

How can I escape ä,ü,ö,ß and the blanks? I tried the java.net.URLEncoder with ISO-8859-1 as encoding without success :(

regards igor

You need to URL-encode the invididual request parameter value with UTF-8, not the entire URL and also not with 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";

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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