1

I've read many threads but really so far none of them helped. I am trying to download and parse JSON which contains some UTF8 characters. Everything works fine except for encoding of those syntaxes. Could you please help me?

HttpGet request = new HttpGet(uri);
HttpResponse resp = client.execute(request);
entity = resp.getEntity();
fos = context.openFileOutput(fileSavePath, Context.MODE_PRIVATE); //fos is FileOutputStream
entity.writeTo(fos);
entity.consumeContent();
fos.flush();
fos.close();

and then I open the file like this:

stream = context.openFileInput(AppConfig.CATEGORIES_JSON_PATH);
reader = new InputStreamReader(stream, StringUtils.UTF8);

I believe that gson stuff is correct and only opening and saving the file matter. I also believe that the mistake lies in entity.writeTo(fos);, cause none encoding is specified. So i tried writingwith byte buffer and OutputStreamWriter, but then gson threw MalformedJsonException: Untermineted string...

any help is highly appreciated

Jakub Pilimon
  • 131
  • 1
  • 10
  • The accepted answer to [this question](http://stackoverflow.com/questions/1001540/how-to-write-a-utf-8-file-with-java) might be helpful. `openFileOutput()` doesn't let you specify an encoding, but you can wrap the resulting `FileOutputSteam` in an `OutputStreamWriter` whose constructor does take an encoding. – acj Oct 10 '12 at 20:41
  • I wrote at the end of my post that I already tried that. No success – Jakub Pilimon Oct 10 '12 at 21:24
  • Your post mentions using a byte buffer and `OutputStreamWriter`, which is slightly different from what I suggested. Have you tried doing `new OutputStreamWriter(fos, "UTF-8")` and writing to that object instead? – acj Oct 10 '12 at 22:11
  • Yes i did. I also tried just wrapping the for in OutputStreamWriter – Jakub Pilimon Oct 11 '12 at 21:13

1 Answers1

0

The problem solved itself. Although while looking up sqlite database the characters were incorrect evrything looks fine in the app.

Jakub Pilimon
  • 131
  • 1
  • 10