java - 解析Json阿拉伯语文本

标签 java android json unicode utf-8

我无法从 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;

}

}

最佳答案

我已经研发了一天左右,终于成功解析了使用以下代码从服务器获取的阿拉伯语 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 来满足您的进一步要求。

谢谢

关于java - 解析Json阿拉伯语文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16547605/

相关文章:

javascript - 如何将点击的视频 block 打开到弹出模式?

python - 如何使用 Lark 解析无效的 JSON?

mysql - 在 MySQL 中以 JSON 格式存储数据

java - 使用 keycloak-admin-client 创建用户期间出现 403

Android SDKTools r21 库错误

android - 如何知道哪个手机支持哪个布局(hdpi、mdpi 和 xhpi)?

java - Android SDK,操作栏颜色

java - 递归,数组列表

java - 避免使用 ProGuard 直接翻译 "public static final"

JavaFx 2.x TableView 绑定(bind)列