将JSON中的特殊字符(如äöüß)进行编码

4

我现在正在开发一款德语应用程序,它从JSON中获取数据并将其显示在ListView中。由于其中包含一些特殊字符,例如üöäß,在我的以下代码中,这些字符会显示为?

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个回答

9

JSON内容默认使用UTF-8字符集(参见RFC 4627第3章)。您需要确保服务器返回正确编码的响应,并明确使用UTF-8编码进行流读取:

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

3
尝试明确为InputStreamReader定义编码,例如:
String encoding = "ISO-8859-1";
BufferedReader reader = new BufferedReader(new InputStreamReader(is, encoding));

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

谢谢 Deepak


0
这是您的URL返回的响应头:
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头缺少字符集声明。如果您可以更改它,那么它应该可以工作。

编辑:如果您无法更改它,则需要在InputStreamReader的第二个参数中硬编码字符集。


此外,JSON内容必须为UTF-8编码。参见:http://stackoverflow.com/questions/5536140/android-parsing-special-characters-a-o-u-in-json - Olegas

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接