如何在布局中制作YouTube RSS Feed?

时间:2014-11-22 21:28:04

标签: android rss

最后编辑:

我已经通过我的YOUTUBE视频在云服务上创建了一个XML文件,并且没有让你的Y BO的JSON与BLA BLA BLA相遇......:D!谢谢大家,所以我感谢这个STACKOVERFLOW COMUNITY。

(对我的英语抱歉,我说西班牙语)

我想从RSS提要(RSS:URL)做一个ListView。

我尝试了很多教程或示例,但不,我仍然不理解。

好的,我想要做的是一个" YouTube频道的上传视频"的ListView,我希望它有图像,标题,视频描述和链接(因为他们点击进入视频)。一个刷新按钮就可以了,一个DB可以保存缓存的图像和文本以供离线查看。

我是新手,我想了解更多,但我很慢,而且我不理解我所看过的任何教程。

我希望至少你能解释我,如果你理解,如果你已经在列表视图中有一个RSS阅读器,请发送给我,以了解你是如何做到的。

如果我有一些你可以说我错的话,我会更新这个。

(----------编辑:18/11/2014 ----------)

我已经在其他帖子中对此进行了编辑,但是由于显示信息不足而关闭..所以..如果你能回答我,我会转发它。(我仍在寻找答案,但我迷路了.. ..

(----------编辑:22/11/2014 ----------)

最近,我找到了一个"喜欢"解决方案,但在教程中搜索相同的解决方案,说它必须是一个Web树(XML),但是,YouTube给它作为..我不知道..不能被一个项目引导的项目普通的RSS。

这里是你的代码..

主要活动类



package com.tibi4.alonnso;

import java.util.List;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.tibi4.alonnso.RssItem;
import com.tibi4.alonnso.ListListener;
import com.tibi4.alonnso.RssReader;

/**
 * Main application activity.
 *
 * Update: Downloading RSS data in an async task
 *
 * @author ITCuties
 *
 */
public class Videos extends Activity {

    // A reference to the local object
    private Videos local;

    /**
     * This method creates main application view
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set view
        setContentView(R.layout.activity_videos);

        // Set reference to this activity
        local = this;

        GetRSSDataTask task = new GetRSSDataTask();

        // Start download RSS task
        task.execute("http://gdata.youtube.com/feeds/api/users/alonsonico10/uploads/?alt=json");

        // Debug the thread name
        Log.d("ITCRssReader", Thread.currentThread().getName());
    }

    private class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem> > {
        @Override
        protected List<RssItem> doInBackground(String... urls) {

            // Debug the task thread name
            Log.d("ITCRssReader", Thread.currentThread().getName());

            try {
                // Create RSS reader
                RssReader rssReader = new RssReader(urls[0]);

                // Parse RSS, get items
                return rssReader.getItems();

            } catch (Exception e) {
                Log.e("ITCRssReader", e.getMessage());
            }

            return null;
        }

        @Override
        protected void onPostExecute(List<RssItem> result) {

            // Get a ListView from main view
            ListView itcItems = (ListView) findViewById(R.id.listView);

            // Create a list adapter
            ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(local,android.R.layout.simple_list_item_1, result);
            // Set list adapter for the ListView
            itcItems.setAdapter(adapter);

            // Set list view item click listener
            itcItems.setOnItemClickListener(new ListListener(result, local));
        }
    }
}
&#13;
&#13;
&#13;

要显示的RSSITEM

&#13;
&#13;
package com.tibi4.alonnso;

public class RssItem {
    // item title
    private String title;
    // item link
    private String link;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getLink() {
        return link;
    }
    public void setLink(String link) {
        this.link = link;
    }
    @Override
    public String toString() {
        return title;
    }
}
&#13;
&#13;
&#13;

LISTLISTENER LISTENER

&#13;
&#13;
package com.tibi4.alonnso;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import com.tibi4.alonnso.RssItem;
/**
 * Class implements a list listener.
 * @author ITCuties
 */
public class ListListener implements OnItemClickListener {
    // Our listener will contain a reference to the list of RSS Items
    // List item's reference
    List<RssItem> listItems;
    // And a reference to a calling activity
    // Calling activity reference
    Activity activity;
    /** We will set those references in our constructor.*/
    public ListListener(List<RssItem> aListItems, Activity anActivity) {
        listItems = aListItems;
        activity  = anActivity;
    }

    /** Start a browser with url from the rss item.*/
    public void onItemClick(AdapterView parent, View view, int pos, long id) {
        // We create an Intent which is going to display data
        Intent i = new Intent(Intent.ACTION_VIEW);
        // We have to set data for our new Intent
        i.setData(Uri.parse(listItems.get(pos).getLink()));
        // And start activity with our Intent
        activity.startActivity(i);
    }
}
&#13;
&#13;
&#13;

RSSREADER READER

&#13;
&#13;
package com.tibi4.alonnso;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import com.tibi4.alonnso.RssItem;

/**
 * Class reads RSS data.
 * @author ITCuties
 */
public class RssReader {
    // Our class has an attribute which represents RSS Feed URL
    private String rssUrl;
    /**
     * We set this URL with the constructor
     */
    public RssReader(String rssUrl) {
        this.rssUrl = rssUrl;
    }
    /**
     * Get RSS items. This method will be called to get the parsing process result.
     * @return
     */
    public List<RssItem> getItems() throws Exception {
        // At first we need to get an SAX Parser Factory object
        SAXParserFactory factory = SAXParserFactory.newInstance();
        // Using factory we create a new SAX Parser instance
        SAXParser saxParser = factory.newSAXParser();
        // We need the SAX parser handler object
        RssParserHandler handler = new RssParserHandler();
        // We call the method parsing our RSS Feed
        saxParser.parse(rssUrl, handler);
        // The result of the parsing process is being stored in the handler object
        return handler.getItems();
    }
}
&#13;
&#13;
&#13;

THE RSSPARSERHANDLER PARSER

&#13;
&#13;
package com.tibi4.alonnso;

import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.tibi4.alonnso.RssItem;

/**
 * Created by Alonnso on 22/11/2014.
 */
public class RssParserHandler extends DefaultHandler {

    // List of items parsed
    private List<RssItem> rssItems;
    // We have a local reference to an object which is constructed while parser is working on an item tag
    // Used to reference item while parsing
    private RssItem currentItem;
    // We have two indicators which are used to differentiate whether a tag title or link is being processed by the parser
    // Parsing title indicator
    private boolean parsingTitle;
    // Parsing link indicator
    private boolean parsingLink;

    public RssParserHandler() {
        rssItems = new ArrayList();
    }
    // We have an access method which returns a list of items that are read from the RSS feed. This method will be called when parsing is done.
    public List<RssItem> getItems() {
        return rssItems;
    }
    // The StartElement method creates an empty RssItem object when an item start tag is being processed. When a title or link tag are being processed appropriate indicators are set to true.
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if ("item".equals(qName)) {
            currentItem = new RssItem();
        } else if ("title".equals(qName)) {
            parsingTitle = true;
        } else if ("link".equals(qName)) {
            parsingLink = true;
        }
    }
    // The EndElement method adds the  current RssItem to the list when a closing item tag is processed. It sets appropriate indicators to false -  when title and link closing tags are processed
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if ("item".equals(qName)) {
            rssItems.add(currentItem);
            currentItem = null;
        } else if ("title".equals(qName)) {
            parsingTitle = false;
        } else if ("link".equals(qName)) {
            parsingLink = false;
        }
    }
    // Characters method fills current RssItem object with data when title and link tag content is being processed
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (parsingTitle) {
            if (currentItem != null)
                currentItem.setTitle(new String(ch, start, length));
        } else if (parsingLink) {
            if (currentItem != null) {
                currentItem.setLink(new String(ch, start, length));
                parsingLink = false;
            }
        }
    }
}
&#13;
&#13;
&#13;

这与其他RSS URL编码,并且工作正常,但是&#34; YOUTUBE&#34; RSS给我带来麻烦的东西。

(----------编辑:22/11/2014 ----------)

0 个答案:

没有答案