Skip to content Skip to sidebar Skip to footer

Retrieve RSS Feed And Display It In Text View

I would like to get this link ' ' working. I want to retrieve the RSS feed and display it in a text view, I've tried out the code but doesn't seems to be working. I guess there is

Solution 1:

Use this Codes...!

    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;
    ItemAdapter adapter1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView list = (ListView) findViewById(R.id.list);
        title = new ArrayList<String>();
        description = new ArrayList<String>();  

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

        adapter1 = new ItemAdapter(this);
        list.setAdapter(adapter1);
    }


    class ItemAdapter extends BaseAdapter {

        final LayoutInflater mInflater;

        private class ViewHolder {
            public TextView title_text;
            public TextView des_text;
        }

        public ItemAdapter(Context context) {
            // TODO Auto-generated constructor stub
            super();
            mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);     
        }

        //@Override
        public int getCount() {
            return title.size();
        }

        //@Override
        public Object getItem(int position) {
            return position;
        }

        //@Override
        public long getItemId(int position) {
            return position;
        }

        //@Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view = convertView;
            final ViewHolder holder;
            if (convertView == null) {
                view = mInflater.inflate(R.layout.mainpage_listitem_activity, parent, false);
                holder = new ViewHolder();
                holder.title_text = (TextView) view.findViewById(R.id.title_text);
                holder.des_text = (TextView) view.findViewById(R.id.des_text);

                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            holder.title_text.setText(""+title.get(position));

            holder.des_text.setText(""+Html.fromHtml(description.get(position)));

        return view;
        }
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >


    <ListView 
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
                >

    </ListView>
</LinearLayout>

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>

Solution 2:

Make a AsyncTask like this

public class MyAsyncTask extends AsyncTask<Void, Void, Void> {
        ProgressDialog dialog;
        MyWeather weatherResult;

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            dialog = new ProgressDialog(MainActivity.this);
            dialog.setMessage("Please wait...");
            dialog.show();
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {
            String weatherString = QueryYahooWeather();
            Document weatherDoc = convertStringToDocument(weatherString);

            weatherResult = parseWeather(weatherDoc);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub

            dialog.dismiss();
            psi.setText(weatherResult.toString());
            super.onPostExecute(result);
        }

    }

and change your onCreate like this

@Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     psi = (TextView)findViewById(R.id.psi);


     new MyAsyncTask().execute();
 }

DO THIS

  • Show a progress dialog in onPreExecute
  • Do network call and parsing in doInBackground
  • Update the view in onPostExecute e.g Show results, dismiss dialog

Post a Comment for "Retrieve RSS Feed And Display It In Text View"