This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes [JAVA] Endoding an InputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "[JAVA] Endoding an InputStream" Watch "[JAVA] Endoding an InputStream" New topic
Author

[JAVA] Endoding an InputStream

Adriano Bellavita
Ranch Hand

Joined: Mar 11, 2010
Posts: 37

Hello,

I have a problem with encoding in my project.

I am developing a proxy, I get remote files. xml.

Some of these have UTF-8 encoding (the type of coding that I intend to return to the client).

Other, different encoding (ISO-8859-1).

The xml returned by remote is converted to json from inputStream in this way.

JSON json = new XMLSerializer (). readFromStream (res.getEntity (). getContent ());


In particular,

Code:
res.getEntity (). getContent ()


returns, as mentioned, the inputStream of the response.

I simply wish to specify the inputStream encoding , so that the json obtained from the XML is correctly displayed in my client that uses encoding as UTF-8.

Specifically, without being able to change the charset, the client (UTF-8) does not correctly display the special characters and accented in case the remote XML has the ISO.


Good day,

Adriano.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41150
    
  45
Streams do not have an encoding - they're simply a sequence of bytes. The XMLSerializer object had better be smart enough to discover the encoding of the XML in the stream (XML has a mechanism for specifying the encoding).

I think the problem is more likely to originate while displaying those characters - how and where are you doing that?


Ping & DNS - my free Android networking tools app
Adriano Bellavita
Ranch Hand

Joined: Mar 11, 2010
Posts: 37
I do the following:

I take an InputStream from a http response.



I use the calss XmlSerializable to get a json.



I give back the JSON serialized to the customer.



toJson the method returns an object of type JSONObject.

When the xml that is returned by the server has an encoding ISO.8859-1, the client does not correctly display accented letters and special characters.

Otherwise, if the XML encoding UTF-8, everything goes well.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41150
    
  45
From the code snippets you posted it's not clear where the problem could arise. For example, what is the XMLSerializer class? It's not the Xerces class, as that has no readFromStream method. And how does the "json" object become a "baos" object? That process must involve specifying an encoding.

Is the client capable of displaying ISO-8859-1 correctly? How are you telling the client that ISO-8859-1 is being used?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [JAVA] Endoding an InputStream
 
Similar Threads
Need to add an invalid UTF-8 character
Setting encoding in web.xml
Handling Special Characters
Unicode Char Problem
Handling Special Characters