【问题标题】:How to retrieve RSS feed? [duplicate]如何检索 RSS 提要? [复制]
【发布时间】:2013-07-25 03:05:36
【问题描述】:

我想从链接app2.nea.gov.sg/data/rss/nea_psi.xml 中检索标题和描述。这是一个 RSS 提要。我该怎么做?

我的代码:

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /** Create a new layout to display the view */
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(1);

        /** Create a new textview array to display the results */
        TextView name[];
        TextView website[];


        try {

            URL url = new URL(
                    "http://app2.nea.gov.sg/data/rss/nea_psi.xml");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();

            NodeList nodeList = doc.getElementsByTagName("item");

            /** Assign textview array lenght by arraylist size */
            name = new TextView[nodeList.getLength()];
            website = new TextView[nodeList.getLength()];


            for (int i = 0; i < nodeList.getLength(); i++) {

                Node node = nodeList.item(i);

                name[i] = new TextView(this);
                website[i] = new TextView(this);


                Element fstElmnt = (Element) node;
                NodeList nameList = ((Document) fstElmnt).getElementsByTagName("title");
                Element nameElement = (Element) nameList.item(0);
                nameList = ((Node) nameElement).getChildNodes();
                name[i].setText("title = "
                        + ((Node) nameList.item(0)).getNodeValue());

                NodeList websiteList = ((Document) fstElmnt).getElementsByTagName("description");
                Element websiteElement = (Element) websiteList.item(0);
                websiteList = ((Node) websiteElement).getChildNodes();
                website[i].setText("description  = "
                        + ((Node) websiteList.item(0)).getNodeValue());



                layout.addView(name[i]);
                layout.addView(website[i]);


            }
        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }

        /** Set the layout view to display */
        setContentView(layout);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name ="@+id/result"
    />

</RelativeLayout>

【问题讨论】:

    标签: android rss


    【解决方案1】:
    import java.net.URL;
    import java.util.ArrayList;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.InputSource;
    
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.text.Html;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    
    public class XMLParsingDOMExample extends Activity {
    
        ArrayList<String> title;
        ArrayList<String> description;
    
        public TextView title_text;
        public TextView des_text;
    
        //ItemAdapter adapter1;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.mainpage_listitem_activity);
    
            //ListView list = (ListView) findViewById(R.id.list);
            title = new ArrayList<String>();
            description = new ArrayList<String>();  
    
            title_text = (TextView) findViewById(R.id.title_text);
            des_text = (TextView) findViewById(R.id.des_text);
    
            try {
    
                URL url = new URL(
                        "http://app2.nea.gov.sg/data/rss/nea_psi.xml");
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.parse(new InputSource(url.openStream()));
                doc.getDocumentElement().normalize();
    
                NodeList nodeList = doc.getElementsByTagName("item");
                for (int i = 0; i < nodeList.getLength(); i++) {
    
                    Node node = nodeList.item(i);       
    
                    Element fstElmnt = (Element) node;
                    NodeList nameList = fstElmnt.getElementsByTagName("title");
                    Element nameElement = (Element) nameList.item(0);
                    nameList = nameElement.getChildNodes();         
    
                    title.add(""+ ((Node) nameList.item(0)).getNodeValue());
    
                    NodeList websiteList = fstElmnt.getElementsByTagName("description");
                    Element websiteElement = (Element) websiteList.item(0);
                    websiteList = websiteElement.getChildNodes();
    
                    description.add(""+ ((Node) websiteList.item(0)).getNodeValue());           
    
                }
            } catch (Exception e) {
                System.out.println("XML Pasing Excpetion = " + e);
            }
    
    
            title_text.setText(""+title.get(0));
    
            String temp = Html.fromHtml(description.get(0)).toString(); 
            String a[] = temp.split("\\)");
            des_text.setText(""+a[0]+")");
        }
    

    mainpage_listitem_activity.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
         > 
                <TextView 
                    android:id="@+id/title_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"     
                    android:text="title"
                    android:layout_margin="5dp"
                    android:textSize="22dp"
                    android:textColor="#FFFFFF"/>   
    
                 <TextView 
                    android:id="@+id/des_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"     
                    android:gravity="center"
                    android:text="description "
                    android:layout_margin="5dp"
                    android:textSize="18dp"
                    android:textColor="#FFFFFF"/>   
    </LinearLayout>
    

    【讨论】:

    • 我在此处粘贴了完整的源代码,请参阅链接stackoverflow.com/questions/17848510/…
    • 看看我粘贴的代码。是你的意思吗?
    • 我编辑了你的代码看看..!
    • 我看到了.. 谢谢,如果我只想抓住 1 个数据,例如“PSI TIME 2pm”,可以做到吗?
    • String temp = title.get(position);然后 String a[] = temp.split("at");在 a[1] 中,您可以获得日期和时间以及 String b[] = temp.split("and");在 b[0] 中,您可以获得 PSI。
    【解决方案2】:

    使用以下库获取 RSS 提要:

    https://github.com/ahorn/android-rss

    【讨论】:

    • 里面没有我可以跟进的项目
    • 链接?是的,它是一个 rss 库项目,您可以使用它从 xml 链接获取 rss 提要。以 zip 格式下载并按照那里给出的说明进行操作。我自己在一个项目中使用过它。
    • 我做不到。我会再试一次
    猜你喜欢
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多