Frage

I have some RSS feed in XML which I am receiving in my Android app. The idea is simple, its just receiving RSS updates and showing them. I managed everything except showing some right words. The problem is that the Data is written in other language, and my app is on English language (I do not know many about those things). Example:

09:05 KAMIONDŽIJE

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

and things like that.. you see those Ž, č, š letters .. they are displayed as some other unknown-to-me languages .. Does anyone know how to fix this.. I just want it to be as it is written in RSS's XML file, not anyhow changed.

this is my complete parser class:

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


}
War es hilfreich?

Lösung

You should make sure the text is parsed as utf-8. But when you've done that, you'll still have a bunch of weird chars but this is normal... A lot of languages has weird chars. Denmark (my roots) has æøå.

Edit:

Try this:

EntityUtils.toString(httpEntity,"UTF-8");
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top