4

I develop a German App right now, it fetches the data from a JSON to a ListView. Since there are some special characters such as üöäß, with my code below, those characters are displayed as ?.

public class LooserSync extends IntentService {

    public LooserSync() {
        super("LooserSyncService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Database.OpenHelper dbhelper = new Database.OpenHelper(getBaseContext());
        SQLiteDatabase db = dbhelper.getWritableDatabase();
        DefaultHttpClient httpClient = new DefaultHttpClient();
        db.beginTransaction();
        HttpGet request = new HttpGet(
                "http://liebenwald.spendino.net/admanager/dev/android/projects.json");
        try {
            HttpResponse response = httpClient.execute(request);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                InputStream instream = response.getEntity().getContent();
                BufferedReader r = new BufferedReader(new InputStreamReader(
                        instream), 8000);
                StringBuilder total = new StringBuilder();
                String line;
                while ((line = r.readLine()) != null) {
                    total.append(line);
                }
                instream.close();
                String bufstring = total.toString();
                JSONArray arr = new JSONArray(bufstring);
                Database.Tables tab = Database.Tables.AllTables.get(Database.Project.NAME);
                tab.DeleteAll(db);
                for (int i = 0; i < arr.length(); i++) {
                    tab.InsertJSON(db, (JSONObject) arr.get(i));
                }
                db.setTransactionSuccessful();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        db.endTransaction();
        db.close();

    }

}
halfer
  • 19,824
  • 17
  • 99
  • 186
hectichavana
  • 1,436
  • 13
  • 41
  • 71

5 Answers5

9

JSON content uses UTF-8 as default character set (see the RFC 4627, chapter 3). You have to make sure that the server returns a response with the right encoding AND to explicitly use the UTF-8 encoding for the stream reader:

BufferedReader r = new BufferedReader(new InputStreamReader(instream, "UTF-8"), 8000);
Laurent Etiemble
  • 27,111
  • 5
  • 56
  • 81
3

Try to explicitly define an encoding for the InputStreamReader, for example:

String encoding = "ISO-8859-1";
BufferedReader reader = new BufferedReader(new InputStreamReader(is, encoding));
Jörg
  • 2,441
  • 1
  • 20
  • 18
1

Use the following code to get the german data as the data is in UTF-8 standrd

HttpGet request = new HttpGet(
                "http://liebenwald.spendino.net/admanager/dev/android/projects.json");

httpget.setHeader("charset", "utf-8");
        ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
    public String handleResponse(final HttpResponse response)
        throws HttpResponseException, IOException {
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() >= 300) {
            throw new HttpResponseException(statusLine.getStatusCode(),
                    statusLine.getReasonPhrase());
        }

        HttpEntity entity = response.getEntity();
        return entity == null ? null : EntityUtils.toString(entity, "UTF-8");
    }
} 

        String html = httpclient.execute(request, responseHandler);

Thanks Deepak

Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
0

This is the response header your URL returns:

Accept-Ranges:none
Connection:Keep-Alive
Content-Length:14572
Content-Type:text/plain
Date:Thu, 26 May 2011 11:47:52 GMT
ETag:"404f6-38ec-4a42a184b1c80;4a0b4ae611cc0"
Keep-Alive:timeout=15, max=100
Last-Modified:Thu, 26 May 2011 09:03:30 GMT
Server:Apache

The Content-Type header is lacking a charset statement. If you can change that, then it should work.

EDIT: If you can't change that, you'll need to hardcode a charset as the second argument of the InputStreamReader.

RoToRa
  • 37,635
  • 12
  • 69
  • 105
  • Also, JSON-content must be in UTF-8. See also: http://stackoverflow.com/questions/5536140/android-parsing-special-characters-a-o-u-in-json – Olegas May 26 '11 at 11:51
0

Possible dupe of: Android Java UTF-8 HttpClient Problem.

In general, if what you're expecting isn't an ASCII character you need to think about encoding; what the server is telling you is the encoding, and how to decode it correctly.

Community
  • 1
  • 1
Asim Ihsan
  • 1,501
  • 8
  • 18