【问题标题】:Encode special characters from JSON (äöüß)从 JSON 编码特殊字符 (äöüß)
【发布时间】:2020-02-29 19:32:41
【问题描述】:

我现在正在开发一个德国应用程序,它将数据从 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();

    }

}

【问题讨论】:

    标签: android json encoding


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      尝试为 InputStreamReader 显式定义一个编码,例如:

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

      【讨论】:

        【解决方案3】:

        使用以下代码获取德语数据,因为数据是 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);
        

        谢谢 迪帕克

        【讨论】:

          【解决方案4】:

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

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

          【讨论】:

            【解决方案5】:

            这是您的 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 的第二个参数。

            【讨论】:

            猜你喜欢
            • 2016-02-26
            • 2021-01-25
            • 1970-01-01
            • 1970-01-01
            • 2013-09-08
            • 2011-12-28
            • 2016-10-19
            • 1970-01-01
            相关资源
            最近更新 更多