编码JSON(äöüß)中的特殊字符

时间:2011-05-26 11:41:54

标签: android json encoding

我现在开发一个德语应用程序,它将数据从JSON提取到ListView。由于有一些特殊字符,例如üöäß,我的代码如下,这些字符显示为

有人可以帮我解决问题吗? THX

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();

    }

}

5 个答案:

答案 0 :(得分:8)

JSON内容使用UTF-8作为默认字符集(请参阅the RFC 4627, chapter 3)。您必须确保服务器返回具有正确编码AND的响应,以明确使用流读取器的UTF-8编码:

BufferedReader r = new BufferedReader(new InputStreamReader(instream, "UTF-8"), 8000);

答案 1 :(得分:2)

尝试显式定义InputStreamReader的编码,例如:

String encoding = "ISO-8859-1";
BufferedReader reader = new BufferedReader(new InputStreamReader(is, encoding));

答案 2 :(得分:1)

使用以下代码获取德语数据,因为数据采用UTF-8标准

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);

由于 迪帕克

答案 3 :(得分:0)

这是您的网址返回的响应标头:

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

Content-Type标头缺少charset语句。如果你可以改变它,那么它应该有效。

编辑:如果你不能改变它,你需要将一个字符集硬编码为InputStreamReader的第二个参数。

答案 4 :(得分:0)

可能的欺骗:Android Java UTF-8 HttpClient Problem

一般来说,如果你期望的不是an ASCII character,你需要考虑编码;服务器告诉你的是编码,以及如何正确解码。