【问题标题】:Android how to get feeds from a .aspx feed?Android 如何从 .aspx 提要中获取提要?
【发布时间】:2012-05-16 11:54:53
【问题描述】:

当我尝试从 .aspx 提要中读取数据时,该应用程序给了我一个错误。 我认为是我使用的 XmlParser 文件。与 .xml 提要一起工作正常,但是当我尝试使用它来读取该 .aspx 提要时,只会给出错误。

有什么好办法吗?

谢谢。

错误信息是:05-16 13:30:31.296: E/Error:(1117): PI不能以xml开头(位置:未知xm@1:7 in java.io.StringReader@2fe98718)

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();
    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  ){
                     return child.getNodeValue();
                 }
             }
         }
     }
     return "";
 }

 /**
  * 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));
    }

}

【问题讨论】:

  • 你能发布错误信息吗?
  • 嗨 Ancantus,我已经编辑了控制台错误消息的问题。谢谢

标签: android rss feed


【解决方案1】:

好的,根据我的搜索。它看起来是 BOM 编码错误。看看这 2 个解决方案,看看它们是否能解决您的问题。

UTF-8 byte-order-mark

Discard the xml header

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多