简体   繁体   中英

How to add multiple feeds into an RSS feed android app?

I'm creating an android app which includes an RSS feed. At the moment, I've programmed the app to fetch the data from one xml feed and to show how I'd like to. However, I'm not sure how I could add multiple xml feeds to my program. I'd like the app to show data from more than one feed/link. I'm currently pulling my single feed directly from the web URL.

I'm a beginner at Java programming and with Android Studio so that's probably why I can't figure this out! Someone suggested a txt file to hold all the feed links, but I'm not sure how I would code this.

If you need any more than the files below then let me know. I think I've included the relevant files.

Apologies for being nooby, any help would be massively appreciated!

ReadRSS.java:

package com.example.liutaurasmazonas.cslogintrying;

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;
    String address="https://www.bloomberg.com/feeds/podcasts/etf_report.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(Document data) {
       if (data != null){
           feedItems=new ArrayList<>();
           Element root=data.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("itunes:summary")){
                           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 Document Getdata(){
        try {
            url=new URL(address);
            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);
            return xmlDoc;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

MyAdapter.java

package com.example.liutaurasmazonas.cslogintrying;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;

import org.w3c.dom.Text;

import java.util.ArrayList;

/**
 * Created by Joe on 05/03/2017.
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
    ArrayList<FeedItem>feedItems;
    Context context;
    public MyAdapter(Context context,ArrayList<FeedItem>feedItems){
        this.feedItems=feedItems;
        this.context=context;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.custum_row_news_item,parent,false);
        MyViewHolder holder=new MyViewHolder(view);
        return holder;

    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        YoYo.with(Techniques.FadeIn).playOn(holder.cardView);
        final FeedItem current=feedItems.get(position);
        holder.Title.setText(current.getTitle());
        holder.Description.setText(current.getDescription());
        holder.Date.setText(current.getPubDate());
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, NewsDetails.class);
                intent.putExtra("Link", current.getLink());
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return feedItems.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView Title,Description,Date;
        CardView cardView;
        public MyViewHolder(View itemView) {
            super(itemView);
            Title= (TextView) itemView.findViewById(R.id.title_text);
            Description=(TextView) itemView.findViewById(R.id.description_text);
            Date=(TextView) itemView.findViewById(R.id.date_text);
            cardView= (CardView) itemView.findViewById(R.id.cardview);
        }
    }
}

You can simply add another RecyclerView to your XML Layout, where you want to show multiple feeds, and then use another Asynctask and Adapter to load the feeds from a different URL on the new RecyclerView.

Like this,

public class ReadRssFromUrl extends AsyncTask<Void,Void,Void>{
ArrayList<FeedItem>feedItems;
RecyclerView recyclerView1;
Context context;
String address="aDifferentUrl.xml";
ProgressDialog progressDialog;
URL url;
public ReadRss(Context context, RecyclerView recyclerView1){
    this.recyclerView1=recyclerView1;
    this.context=context;
    progressDialog=new ProgressDialog(context);
    progressDialog.setMessage("Loading...");
}

So the RecyclerView you pass to this constructor should be the new one.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM