【问题标题】:Getting hebrew character from http response in json on android从android上的json中的http响应获取希伯来语字符
【发布时间】:2012-08-17 10:20:02
【问题描述】:

我正在做一个项目,我需要从我的应用引擎服务器获取一些包含希伯来字符的数据(数据以 json 格式发送)。

在服务器端:

resp.setHeader("Content-Type", "application/json; charset=UTF-8");
PrintWriter out = resp.getWriter();
out.print(responeData.toString());

当我调试服务器时,我看到响应数据看起来很好(意思是,它显示了我的希伯来字符。

在客户端(安卓):

执行此代码后,我得到的 resultData 是 ???而是希伯来字符。 我尝试了所有不同的编码,例如“windows-1255”、“iso-8859-8” 有谁知道问题是什么? 谢谢!

// Create new default http client
    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout Limit
    HttpConnectionParams.setSoTimeout(client.getParams(), 10000);
    HttpResponse response;
    HttpPost post = new HttpPost(serviceURL);

    try {
        StringEntity se = new StringEntity(requestPayload.toString());
        post.addHeader(CustomHeader.TASK_NAME.getHeaderName(), taskName);
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        post.setEntity(se);

        // Execute the request
        response = client.execute(post);

        // Get the response status code
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();

        if (statusCode == 200) { // Ok
            if (response != null) { // Checking response
                InputStream in = response.getEntity().getContent(); // Get the data in the entity
                retreturnVal = HttpCaller.readContentFromIS(in);
            }
        }
    } catch (Exception e) {
        Log.e("Error in connectivity layer, stacktrace: ", e.toString());
        return null;
    }

    public static String readContentFromIS(InputStream in) throws IOException {
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(in, "UTF-8"), 8);
    StringBuffer sb = new StringBuffer();
    String line = "";
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    return sb.toString();       
}

【问题讨论】:

    标签: android json encoding


    【解决方案1】:

    假设您在服务器上使用ServletResponse,而不是调用

    resp.setHeader("Content-Type", "application/json; charset=UTF-8");
    

    你应该打电话

    resp.setContentType("application/json; charset=UTF-8");
    

    这应该具有将调用中使用的编码更改为getWriter() 到UTF-8 的效果。我不认为调用setHeader 有同样的副作用。

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,但我做到了

       StringEntity se = new StringEntity(requestPayload.toString(), "UTF-8");
      

      而不是

       StringEntity se = new StringEntity(requestPayload.toString());
      

      它对我来说很好,这是另一种解决方案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-02
        • 1970-01-01
        • 2012-01-12
        相关资源
        最近更新 更多