【问题标题】:language in application AndroidAndroid应用程序中的语言
【发布时间】:2012-08-11 17:00:26
【问题描述】:

我在我的 Android 应用程序中收到了一些 XML 格式的 RSS 提要。这个想法很简单,它只是接收 RSS 更新并显示它们。除了显示一些正确的词外,我管理了一切。问题是数据是用其他语言编写的,而我的应用程序是英文的(我对这些东西知之甚少)。示例:

09:05 KAMIONDŽIJE

20:05 Doček naših olimpijaca ispred Skupštine grada

和类似的东西..你看到那些Ž,č,š字母..它们显示为其他一些我不知道的语言..有谁知道如何解决这个问题..我只是希望它是它是用 RSS 的 XML 文件编写的,没有任何改变。

这是我完整的解析器类:

public class XMLParser {

// constructor
public XMLParser() {

}

/**
 * Getting XML from URL making HTTP request
 * @param url string
 * */
public String getXmlFromUrl(String url) {
    String xml = null;

    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // return XML
    return xml;
}
/**
 * Getting XML DOM element
 * @param XML string
 * */

public Document getDomElement(String xml){
    Document doc = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setCoalescing(true);
    dbf.setNamespaceAware(true);
    try {
        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is); 

        } catch (ParserConfigurationException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (SAXException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (IOException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }

        return doc;
}

/** Getting node value
  * @param elem element
  */
 public final String getElementValue( Node elem ) {
     Node child;
     if( elem != null){
         if (elem.hasChildNodes()){
             for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
                 //if( child.getNodeType() == Node.TEXT_NODE  ){
                 if(child.getNodeType() == Node.TEXT_NODE || child.getNodeType() == Node.CDATA_SECTION_NODE){
                     return child.getNodeValue();
                 }
             }
         }
     }
     return "";
     //return elem.getTextContent();
 }

 /**
  * Getting node value
  * @param Element node
  * @param key string
  * */
 public String getValue(Element item, String str) {     
        NodeList n = item.getElementsByTagName(str);                
        return this.getElementValue(n.item(0));
    }
 public String getValue2(Element item, String str){
     //NodeList n = item.getElementsByTagNameNS("http://purl.org/rss/1.0/modules/content/", str);
     return item.getTextContent();
 }


}

【问题讨论】:

    标签: android encoding letters


    【解决方案1】:

    您应该确保文本被解析为 utf-8。但是当你这样做时,你仍然会有一堆奇怪的字符,但这是正常的......很多语言都有奇怪的字符。丹麦(我的根)有æøå。

    编辑

    试试这个:

    EntityUtils.toString(httpEntity,"UTF-8");
    

    【讨论】:

    • 如何检查它是否在 utf-8 中解析? .. 我知道这些词是什么意思,它们是我祖国的话(塞尔维亚),但我只是希望它们能直接显示在应用程序中。
    • 那么您需要在读取数据的位置显示您的代码。
    • 好的,我编辑了我的帖子..检查一下,注意 getValue2 用于 东西,getValue1 用于普通 东西..
    猜你喜欢
    • 1970-01-01
    • 2012-06-24
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    相关资源
    最近更新 更多