1

如何获取 json 格式的阿拉伯字符串以及如何在 android 应用程序 inputStreamReader 中显示。我从服务器端获取 json 并使用 Windows-1256 encodingString 转换阿拉伯字符串,但某些文本无法正确显示。

HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            System.out.println(url + ":::url");
            try {
                HttpResponse httpResponse = httpClient.execute(httpGet);
                InputStream inputStream = httpResponse.getEntity()
                        .getContent();
                InputStreamReader inputStreamReader = new InputStreamReader(
                        inputStream,"windows-1256");
                        //new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(
                        inputStreamReader,8);
                StringBuilder stringBuilder = new StringBuilder();
                String bufferedStrChunk = null;
                while ((bufferedStrChunk = bufferedReader.readLine()) != null) {
                    stringBuilder.append(bufferedStrChunk);
                }

                return stringBuilder.toString();
            } catch (ClientProtocolException cpe) {
                System.out
                        .println("Exception generates caz of httpResponse :"
                                + cpe);
                cpe.printStackTrace();
            } catch (IOException ioe) {
                System.out
                        .println("Second exception generates caz of httpResponse :"
                                + ioe);

                ioe.printStackTrace();
            }
4

1 回答 1

1

我已经研发了一天左右,终于成功地解析了我使用以下代码从服务器获取的阿拉伯 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-19T10:13:46.370 回答