如何使用关键字过滤RSS Feed?

时间:2017-04-11 11:06:26

标签: java android rss

我已经构建了一个带有RSS源的Android应用程序作为主页。我的应用程序的一个功能是让多个用户拥有不同的首选项。 用户是否可以输入他们感兴趣的关键字,然后使用这些关键字来过滤RSS提要的新闻? 我对android编程很新,我完全坚持这个。 我真的很喜欢任何帮助或想法!

我的RSS源代码的主要部分(只是询问您是否需要更多代码):

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.provider.DocumentsContract;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import static android.R.attr.data;

/**
 * Created by Joe on 04/03/2017.
 */
public class ReadRss extends AsyncTask<Void,Void,Void>{
    ArrayList<FeedItem>feedItems;
    RecyclerView recyclerView;
    Context context;
    static ArrayList<String>address;

    static {
        address=new ArrayList<>();
        address.add("https://www.bloomberg.com/politics/feeds/site.xml");
        address.add("https://www.bloomberg.com/feeds/podcasts/etf_report.xml");
    }
    //String address="https://www.bloomberg.com/feeds/podcasts/etf_report.xml";
   // String address2="www.bloomberg.com/politics/feeds/site.xml";
    ProgressDialog progressDialog;
    URL url;
    public ReadRss(Context context, RecyclerView recyclerView){
        this.recyclerView=recyclerView;
        this.context=context;
        progressDialog=new ProgressDialog(context);
        progressDialog.setMessage("Loading...");
    }

    @Override
    protected void onPreExecute() {
        progressDialog.show();
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Void aVoid) {

        super.onPostExecute(aVoid);
        progressDialog.dismiss();
        MyAdapter adapter=new MyAdapter(context,feedItems);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.addItemDecoration(new VerticalSpace(50));
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected Void doInBackground(Void... params) {
        ProcessXml(Getdata());
        return null;
    }

    private void ProcessXml(ArrayList<Document> data) {
        if (data != null) {
            feedItems = new ArrayList<>();
            for (Document doc : data) {
                Element root = doc.getDocumentElement();
                Node channel = root.getChildNodes().item(0);
                NodeList items = channel.getChildNodes();
                for (int i = 0; i < items.getLength(); i++) {
                    Node currentchild = items.item(i);
                    if (currentchild.getNodeName().equalsIgnoreCase("item")) {
                        FeedItem item = new FeedItem();
                        NodeList itemchilds = currentchild.getChildNodes();
                        for (int j = 0; j < itemchilds.getLength(); j++) {
                            Node current = itemchilds.item(j);
                            if (current.getNodeName().equalsIgnoreCase("title")) {
                                item.setTitle(current.getTextContent());
                            } else if (current.getNodeName().equalsIgnoreCase("description")) {
                                item.setDescription(current.getTextContent());
                            } else if (current.getNodeName().equalsIgnoreCase("pubDate")) {
                                item.setPubDate(current.getTextContent());
                            } else if (current.getNodeName().equalsIgnoreCase("link")) {
                                item.setLink(current.getTextContent());
                            }

                        }
                        feedItems.add(item);
                        Log.d("itemTitle", item.getTitle());
                        Log.d("itemDescription", item.getTitle());
                        Log.d("itemLink", item.getTitle());
                        Log.d("itemPubDate", item.getTitle());
                    }
                }
            }
        }
    }

    public ArrayList<Document> Getdata(){
        ArrayList<Document> documents = new ArrayList<>();
        for (String addr: address) {
            try {
                url = new URL(addr);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                InputStream inputStream = connection.getInputStream();
                DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = builderFactory.newDocumentBuilder();
                Document xmlDoc = builder.parse(inputStream);
                documents.add(xmlDoc);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return documents;
    }
}

1 个答案:

答案 0 :(得分:2)

您应该使用选定的解析器解析xml文件(您选择了DOM解析,但我强烈建议您检查SimpleXML)。然后用解析的数据填充List。创建新方法,检查对象是否包含/ equals needed标签。

  List<FeedItem> feeds = new ArrayList<>();
    for(FeedItem feedItem : feedItems){
        if (feedItem.getTitle().equals("tag which you want")){
            feeds.add(feedItem);
        }
    }
    return feeds;