Skip to main content

Encoding problem with google geocoding service in Glassfish v3 JavaEE 6 App

2 replies [Last post]
michael7186
Offline
Joined: 2010-04-26

Hi!
I have a serious encoding problem while using Google Maps encoding Service in a Java EE Applikation. I use Glassfish v3 as App Server on Windows 7. Furthermore i use JSF 2.0 and Mojarra 2.0.2, Facelets 1.1.14, Primefaces 2.0, JAAS, JSP API 2.2, Servlet API 2.5 .

I use this code to (reverse)geocode the variable "address".
Example address = "Doggenriedstraße, Weingarten, Germany".

URL url = new URL("http://maps.google.com/maps/geo?q="
+ URLEncoder.encode(address, "UTF-8")
+ "&output=json&sensor=false&key=" + "ABQ...")

When i evaluate the response, special characters like: "ß" or "ä, ö, ü" are encoded in Windows-1252 (subset of ISO-8859-1) or something. Example: "ß" is encoded as "ß". When i use the application on a Mac OS the special characters are also wrong encoded, it is not Windows-1252 but something Mac specific. When i enter the same URL to the Firefox Browser - the response encoding is correct. So it has something to do with Glassfish or Grizzly i don't know.

I have tried much to solve the Problem:
- all files are encoded in utf-8, all xml-like files have the tag <?xml version="1.0" encoding="UTF-8"?>
- in the sun-web.xml i use: ""
- in the web.xml i use:


com.sun.faces.disableUnicodeEscaping
auto

en_US
UTF-8

no
UTF-8

de
UTF-8

- I use a utf8-filter:
[...]
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {

request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);

chain.doFilter(request, response);
}

-> I noticed that the response object's character encoding is "ISO-8859-1" every time the doFilter function is called. In the filter i set it manually to utf-8, that works, but next time the doFilter method is called, the same happens. The request object's character encoding is always utf-8.

So i think the problem has something to do with glassfish or facelets??
Can someone please help me?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
michael7186
Offline
Joined: 2010-04-26

Okay i found the solution...
That was my code:

[i]...
URLConnection connection = url.openConnection();
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

while ((line = reader.readLine()) != null) { builder.append(line);}

String builderString = builder.toString();
...[/i]

Google returned an utf8-string but InputputStreamReader uses the Systems Default Charset which is for example cp1252 for windows.... So the easy Solution is to use:

[i]InputStreamReader(connection.getInputStream(), "UTF-8");[/i]

Damn... thats all!

Cheers, Michael

hwellmann
Offline
Joined: 2008-08-16

Working with URLConnection, URLEncoder etc. is rather low-level. Have you considered using the Jersey Web Service Client API?

See http://blogs.sun.com/enterprisetechtips/entry/consuming_restful_web_serv...

It's part of Glassfish anyway :-)

Cheers,
Harald