Aller au contenu

amelioration lecteur de flux rss


lostsoul

Recommended Posts

Bonjour,

Je débute complètement sur Android.

J'ai récupéré les sources d'un petit lecteur de flux RSS, et j'aurais besoin de modifier certaines choses (j'en ai déjà modifié un peu).

Par exemple, en cliquant sur un flux, je voudrais pouvoir avoir l'article entier et pas simplement le résumé, mais je ne sais pas comment faire cela.

Egalement, lorsqu'un titre possède un accent ou autre, il ne s'affiche pas en entier, et je ne sais pas comment faire.

Je met les codes java, si quelqu'un pouvait me donner des indications sur les points à regarder car je ne sais pas dans quel fichier est géré cela...

package com.Webynuxnet;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Webynuxnet extends Activity {

   public static RssItem selectedRssItem = null;
   String feedUrl = "http://www.webynux.net/feed";
   ListView rssListView = null;
   ArrayAdapter aa = null;

   ArrayList rssItems = RssItem.getRssItems(feedUrl);

   public static final int RssItemDialog = 1;

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       // get the listview from layout.xml
       rssListView = (ListView) findViewById(R.id.rssListView);
       // here we specify what to execute when individual list items clicked
       rssListView.setOnItemClickListener(new OnItemClickListener() {

           @Override
           public void onItemClick(AdapterView<?> av, View view, int index,
                   long arg3) {
               selectedRssItem = rssItems.get(index);

               // we call the other activity that shows a single rss item in
               // one page
               Intent intent = new Intent("com.Webynuxnet.displayRssItem");
               startActivity(intent);
           }
       });

       aa = new ArrayAdapter(this, R.layout.list_item, rssItems);
       rssListView.setAdapter(aa);

       refressRssList();
   }

   protected Dialog onCreateDialog(int id, Bundle args) {
       // TODO Auto-generated method stub
       switch (id) {
       case RssItemDialog: {
           LayoutInflater li = LayoutInflater.from(this);
           View rssDetails = li.inflate(R.layout.rss_details, null);

           AlertDialog.Builder rssDialog = new AlertDialog.Builder(this);
           rssDialog.setTitle("Article Webynux.net");
           rssDialog.setView(rssDetails);

           return rssDialog.create();
       }
       }

       return null;
   }

   protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
       // TODO Auto-generated method stub
       switch (id) {
       case RssItemDialog: {
           AlertDialog rssDialog = (AlertDialog) dialog;

           SimpleDateFormat sdf = new SimpleDateFormat("MM/dd - hh:mm:ss");
           rssDialog.setTitle(selectedRssItem.getTitle() + " : " + sdf.format(selectedRssItem.getPubDate()));

           String text = selectedRssItem.getDescription() + " : " + selectedRssItem.getLink();

           TextView tv = (TextView) rssDialog.findViewById(R.id.rssDetailsTextView);
           tv.setText(text);

       }
       }
   }

   private void refressRssList() {

       ArrayList newItems = RssItem.getRssItems(feedUrl);

       rssItems.clear();
       rssItems.addAll(newItems);

       aa.notifyDataSetChanged();
   }

}

package com.Webynuxnet;



import java.text.SimpleDateFormat;



import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;



public class RssItemDisplayer extends Activity {

   @Override

   public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.rss_item_displayer);



       RssItem selectedRssItem = com.Webynuxnet.Webynuxnet.selectedRssItem;

       TextView titleTv = (TextView)findViewById(R.id.titleTextView);    

       TextView contentTv = (TextView)findViewById(R.id.contentTextView);    



       String title = "";

       SimpleDateFormat sdf = new SimpleDateFormat("MM/dd - hh:mm:ss");

       title = "\n" + selectedRssItem.getTitle() + "   ( "    + sdf.format(selectedRssItem.getPubDate()) + " )\n\n";



       String content = "";

       content += selectedRssItem.getDescription() + "\n" + selectedRssItem.getLink();



       titleTv.setText(title);

       contentTv.setText(content);

   }

}

package com.Webynuxnet;



import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;



import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;



import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;



public class RssItem {



   private String title;

   private String description;

   private Date pubDate;

   private String link;



   public RssItem(String title, String description, Date pubDate, String link) {

       this.title = title;

       this.description = description;

       this.pubDate = pubDate;

       this.link = link;

   }



   public String getTitle() {

       return title;

   }



   public void setTitle(String title) {

       this.title = title;

   }



   public String getDescription() {

       return description;

   }



   public void setDescription(String description) {

       this.description = description;

   }



   public Date getPubDate() {

       return pubDate;

   }



   public void setPubDate(Date pubDate) {

       this.pubDate = pubDate;

   }



   public String getLink() {

       return link;

   }



   public void setLink(String link) {

       this.link = link;

   }



   @Override

   public String toString() {



       SimpleDateFormat sdf = new SimpleDateFormat("MM/dd - hh:mm:ss");



       String result = getTitle() + "   ( " + sdf.format(this.getPubDate()) + " )";

       return result;

   }



   public static ArrayList getRssItems(String feedUrl) {



       ArrayList rssItems = new ArrayList();



       try {

           //open an URL connection make GET to the server and 

           //take xml RSS data

           URL url = new URL(feedUrl);

           HttpURLConnection conn = (HttpURLConnection) url.openConnection();



           if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {

               InputStream is = conn.getInputStream();



               //DocumentBuilderFactory, DocumentBuilder are used for 

               //xml parsing

               DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

               DocumentBuilder db = dbf.newDocumentBuilder();



               //using db (Document Builder) parse xml data and assign

               //it to Element

               Document document = db.parse(is);

               Element element = document.getDocumentElement();



               //take rss nodes to NodeList

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



               if (nodeList.getLength() > 0) {

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



                       //take each entry (corresponds to  tags in 

                       //xml data



                       Element entry = (Element) nodeList.item(i);



                       Element _titleE = (Element) entry.getElementsByTagName("title").item(0);

                       Element _descriptionE = (Element) entry.getElementsByTagName("description").item(0);

                       Element _pubDateE = (Element) entry.getElementsByTagName("pubDate").item(0);

                       Element _linkE = (Element) entry.getElementsByTagName("link").item(0);



                       String _title = _titleE.getFirstChild().getNodeValue();

                       String _description = _descriptionE.getFirstChild().getNodeValue();

                       Date _pubDate = new Date(_pubDateE.getFirstChild().getNodeValue());

                       String _link = _linkE.getFirstChild().getNodeValue();



                       //create RssItemObject and add it to the ArrayList

                       RssItem rssItem = new RssItem(_title, _description,

                               _pubDate, _link);



                       rssItems.add(rssItem);

                   }

               }



           }

       } catch (Exception e) {

       }



       return rssItems;

   }

}

Merci d'avance ;-)

Très cordialement,

lostsoul

Lien vers le commentaire
Partager sur d’autres sites

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

×
×
  • Créer...