2

我无法解析 SQL 数据库中的阿拉伯语/波斯语文本。一切都设置为 UTF-8。我的 SQL 数据库文本设置为utf8_general_ci. JSON 解析器也设置为 UTF-8。

文本以英语显示得很好。但是当我在数据库中使用阿拉伯语/波斯语文本时,android 将文本显示为???????.

public class JSONParser {

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

    // constructor
    public JSONParser() {

}

// function get json from url
// by making HTTP POST or GET method
public JSONObject makeHttpRequest(String url, String method,
        List<NameValuePair> params) {

    // Making HTTP request
    try {

        // check for request method
        if(method == "POST"){
            // request method is POST
            // defaultHttpClient
            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"){
            // request method is 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, "UTF-8"), 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 parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}

}

4

3 回答 3

2

我已经研发了一天左右,终于成功地解析了我使用以下代码从服务器获取的阿拉伯 json 响应。所以,可能对你有帮助。

  HttpParams params = new BasicHttpParams();
  HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
  HttpProtocolParams.setContentCharset(params, "UTF-8");
  params.setBooleanParameter("http.protocol.expect-continue", false);
  HttpClient httpclient = new DefaultHttpClient(params);

  HttpPost httppost = new HttpPost(Your_URL);
  HttpResponse http_response= httpclient.execute(httppost);

  HttpEntity entity = http_response.getEntity();
  String jsonText = EntityUtils.toString(entity, HTTP.UTF_8);

  Log.i("Response", jsonText);

现在,使用 jsonText 来满足您的进一步要求。

谢谢你

于 2014-04-19T09:33:46.487 回答
0

也许问题出在服务器端。检查您从服务器获得的原始字符串以查看其格式是否正确。

于 2013-05-14T16:22:55.213 回答
0

我认为它可以通过将其存储为 clob/blob 来帮助您,因为一旦您在服务器端获得了从 UTF-8 转换的字节,任何客户端代码也可以使用各种字符串编码格式来显示测试。

或者我的其他建议,使用 webview 来显示它,它更成熟地处理这些细微差别。

于 2013-05-24T15:14:49.500 回答