0

Rss フィードに取り組んでいます。DOM パーサーを使用しています。

The actual link is:

http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNEd3ZGNLp5P-6bn44JmKbPfQimTyQ&url=http://www.foxnews.com/world/2012/09/21/libyans-storm-ansar-al-sharia-compound-in-backlash-attack-on-us-consulate/

After executing the function    xml = parser.getXmlFromUrl(URL);
URL=https://news.google.com/news/feeds?ned=us&topic=w&output=rss



In xml Iam getting the link as:

http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNHrclA7rxiQuEvAA_o9ZDRlZQFLkg&url=http://www.usatoday.com/news/world/story/2012/09/21/libyans-storm-militia-in-backlash-of-attack-on-us/57821580/1

この URL をブラウザに貼り付けると、「これは無効な URL です」と表示されます。

How to get the a valid url? Could anyone please help me.

以下の関数は、url から xml を取得することです。

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

2 に答える 2

0

コードの一部を試して、変数に従って変更してください....

try {
    // Create a URL for the desired page
        String stringURL = "http://link;
        URL url = new URL(stringURL);
        // Read all the text returned by the server
        BufferedReader in = new BufferedReader(new InputStreamReader(
                url.openStream()));
        while ((xml = in.readLine()) != null) {
            stringXml += xml;

        }
        in.close();

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(new StringReader(stringXml)));
            doc.getDocumentElement().normalize();
            NodeList nodeLst = doc.getElementsByTagName("nodename");
            Node node = nodeLst.item(0);
            String demo1 = node.getTextContent();

            nodeLst = doc.getElementsByTagName("nodename");
            node = nodeLst.item(0);
            String demo2= node.getTextContent();
            nodeLst = doc.getElementsByTagName("nodename");
            node = nodeLst.item(0);
            String demo3= node.getTextContent();
            nodeLst = doc.getElementsByTagName("nodename");
            node = nodeLst.item(0);
            String demo4= node.getTextContent();
            nodeLst = doc.getElementsByTagName("nodename");
            node = nodeLst.item(0);
            String demo5= node.getTextContent();
            nodeLst = doc.getElementsByTagName("nodename");
            node = nodeLst.item(0);
            String demo6= node.getTextContent();


            nodeLst = doc.getElementsByTagName("nodename");
            node = nodeLst.item(0);

            nodeLst = doc.getElementsByTagName("nodename");
            node = nodeLst.item(0);
            password="+username.getText();
            }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
于 2012-09-22T04:31:49.797 に答える