java - 在 JSON 代码上使用希伯来语?

标签 java mysql json character-encoding hebrew

我正在使用此代码从 MySQL 数据库发布和获取数据。 但是当获取非英文数据时,它显示为问号? 我要进行哪些更改才能使用希伯来语?

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

public JSONParser() {

}

public JSONObject makeHttpRequest(String url, String method,List<NameValuePair> params) {
    try {

        if(method == "POST"){

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        }else if(method == "GET"){
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }           

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    return jObj;

}

最佳答案

确保您的 HTTP 响应以 Unicode(例如 UTF-8)编码输出,并且您的客户端(使用该服务的应用程序)必须知道该编码才能读取您的响应。

关于java - 在 JSON 代码上使用希伯来语?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21911742/

相关文章:

java - 为什么集合框架中没有堆栈接口(interface)?

mysql - 如何使用批处理脚本将.txt 文件中的数据放入 mysql 数据库?

c# - 将 JSON 对象反序列化为嵌套的 C# 对象

mysql - SQL 不同列和 Null

arrays - 在 Elasticsearch 中搜索数组时获取对象命中

php - jquery appendTo 函数在 ajax success 函数中不起作用

java - 向 Apache TelnetClient 发送字节命令

java - 没有可检查的 float 操作按钮

java构建工具maven和jar文件

php - 无法根据 codeigniter 中的其他数据获取数据