2

SQL データベースからアラビア語/ペルシャ語のテキストを解析できません。すべてUTF-8に設定されています。SQL データベース テキストが に設定されていutf8_general_ciます。JSON パーサーも UTF-8 に設定されています。

テキストは英語でうまく表示されます。しかし、データベースでアラビア語/ペルシャ語のテキストを使用すると、アンドロイドはテキストを として表示します???????

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

I have been r & d around a day and finally success to parse my arabic json response getting from server using following code.So, may be helpful to you.

  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);

Now, use jsonText for your further requirement.

Thank You

于 2014-04-19T09:33:46.487 に答える
0

問題はサーバー側にあるのかもしれません。サーバーから取得した生の文字列をチェックして、正しくフォーマットされているかどうかを確認します。

于 2013-05-14T16:22:55.213 に答える