Android: Showing Spanish (among other) characters read from file

I know there are a lot of questions similar to this all around SO, but they either provide a very case-specific solution that I don't get to adapt to my issue or simply don't seem to work at all.

I have a multi-language app that downloads certain information from the internet and stores it into a file for later usage. This is how the storage is done:

public static void writeStringToFile(String string, File file)
        throws IOException {

    if (!file.exists()) {
        file.createNewFile();
    }

    FileOutputStream outputStream = new FileOutputStream(file);
    outputStream.write(string.getBytes("UTF-8"));
    outputStream.close();
}

But later, when the spanish version of the file is read, the app displays the special characters, like ñ, as the black diamond with the question mark inside I-ve tried to:

So I'm almost sure that the problem is in how I write the file since it gets out of the server fine but is stored wrong. But I have been so much time looking at the method and I can't find what the problem is...any clues?

EDIT: This is how I download the information.

    public static InputStream performGetRequest(String uri)
        throws IOException, URISyntaxException, ServerIsCheckingException {
    HttpResponse response;

    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet();
    request.setURI(new URI(uri));
    response = client.execute(request);
    if (response.getStatusLine().getStatusCode() == 409) {
        throw new ServerIsCheckingException();
    }
    else {
        return response.getEntity().getContent();
    }
}

To convert it to a String object that I later pass to the method writeStringToFile, I use

    public static String inputStreamAsString(InputStream is) throws IOException {
    java.util.Scanner s = new java.util.Scanner(is);
    String ret;
    ret = s.useDelimiter("\\A").hasNext() ? s.next() : "";
    return ret;
}

I also thought that writeStringToFile could be the problem, but I tried another alternative which specifies UTF-8 to be used and didn't work either.

Upvotes: 1

Views: 914

Answers (1)

nKn
nKn

Reputation: 13761

You'll have to make sure that the document you are trying to write is being read in the same charset. In your case, if the document you're downliading is in spanish, it will probably be written in UTF-8 or ISO-8859-1, so you'll have to set the corresponding enconding both in the reading and writing.

You might use HttpProtocolParams.setContentCharset() to set the corresponding charset to the BasicHttpParams object.

This might help:

Upvotes: 2

Related Questions