Android - HttpEntity返回“未找到资源”页面

时间:2013-07-12 15:56:17

标签: android xml http xml-parsing

我正在尝试获取和RSS提要。但是,HttpEntity会返回:

                                   资源未找到                                  

找不到资源

              

我使用的地址在我的浏览器中运行良好。

MainActivity.java:

...
String URL = "http://rss.nytimes.com/services/xml/rss/nyt/Africa.xml";
// XML node keys
String KEY_ITEM = "item"; // parent node
String KEY_TITLE = "title";
String KEY_PUBDATE = "pubDate";
String KEY_CREDIT = "media:credit";

XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
mOutput += xml;
Document doc = parser.getDomElement(xml); // getting DOM element

NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
  // creating new HashMap
  HashMap<String, String> map = new HashMap<String, String>();
  Element e = (Element) nl.item(i);
  // adding each child node to HashMap key => value
  map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
  map.put(KEY_PUBDATE, parser.getValue(e, KEY_PUBDATE));
  map.put(KEY_CREDIT, parser.getValue(e, KEY_CREDIT));
}
...

XMLParser.java:

public class XMLParser {
    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();
            Log.d("dsgv", "UnsupportedEncodingException");
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            Log.d("dsgv", "ClientProtocolException");
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("dsgv", "IOException");
        }
        // return XML
        return xml;
    }
    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 DOM
        return doc;
    }
    public String getValue(Element item, String str) {     
        NodeList n = item.getElementsByTagName(str);       
        return this.getElementValue(n.item(0));
    }

    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 "";
    } 
}

1 个答案:

答案 0 :(得分:1)

好的,我找到了答案:

RSS应该使用GET方法获取,而不是POST。

我替换了

HttpPost httpPost = new HttpPost(url);

HttpGet httpGet = new HttpGet(url);