需要在我的Andr​​oid设备展示克罗地亚,西班牙符号 [英] need to show croatian, spanish symbols in my device in android

查看:217
本文介绍了需要在我的Andr​​oid设备展示克罗地亚,西班牙符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从URL获得一个JSON文件,分析它,并获得内容。此JSON包含克罗地亚的字符和符号,像这样的Pošaljite电子马龙。我用下面的code来从URL的JSON文件。这是我使用的URL http://ptracker.com/webteh/localization.php

I need to get a json file from an URL, parse it and get the contents. This Json contains Croatian characters and symbols like this "Pošaljite e-marlon". I used the following code to get the Json file from the URL. This is the URL I used http://ptracker.com/webteh/localization.php

InputStream is = null;
                try {

                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(language_url);

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

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

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

将响应JSON不显示原始字符串Pošaljite电子马龙。它给Poaljite电子mailom。如何解决这一问题?

The response json doesn't show the original String "Pošaljite e-marlon". It gives "Poaljite e-mailom". How to fix this?

推荐答案

我觉得你的问题是你的假设是你的回应是EN ISO使用-8859-2 codeD。尝试检查响应头,看看你能获得的编码描述是这样的:

I think your problem is your are assuming your response is encoded using ISO-8859-2. Try to check the response headers to see if you can obtain the encoding description, something like:

content-type:application/json; charset=UTF-8

更新:我使用的伎俩来获得该字符集:我打开萤火虫,我在控制台运行 document.characterSet 这回Windows的1252。然后我做了这个小例子,它的工作。我不知道此charset采用的是Android支持,但...

UPDATE: I used a trick to get the charset: I opened Firebug and I ran in the console document.characterSet which returned "windows-1252". Then I did this little example and it worked. I'm not sure this charset is supported in Android, but...

public static void main(String[] args) throws IOException {
        URL url= new URL("http://ptracker.com/webteh/localization.php");
        URLConnection con = url.openConnection();
        System.out.println(con.getContentType());
        BufferedReader br= new BufferedReader(new InputStreamReader( 
                  con.getInputStream(),Charset.forName("windows-1252")));
        String s=null;
        while ((s=br.readLine())!=null) {
            System.out.println(s);
        }
    }

这篇关于需要在我的Andr​​oid设备展示克罗地亚,西班牙符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆