Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在我的android应用程序中标记Rss新闻源_Android - Fatal编程技术网

在我的android应用程序中标记Rss新闻源

在我的android应用程序中标记Rss新闻源,android,Android,我是android新手。我正在创建一个应用程序,在该应用程序中,我正在考虑在活动的底部添加一个“MarqueRSS提要”。这可能吗?是否可以在同一活动中放置两个以上的RSS源?是的,这是可能的 下面是从URL获取RSS提要并在android的listview中列出它的代码 您需要首先创建一个扩展ListActivity的类,然后将以下代码放入: // Initializing instance variables headlines = new ArrayList();

我是android新手。我正在创建一个应用程序,在该应用程序中,我正在考虑在活动的底部添加一个“MarqueRSS提要”。这可能吗?是否可以在同一活动中放置两个以上的RSS源?

是的,这是可能的

下面是从URL获取RSS提要并在android的listview中列出它的代码

您需要首先创建一个扩展ListActivity的类,然后将以下代码放入:

    // Initializing instance variables
    headlines = new ArrayList();
    links = new ArrayList();

    try {
        URL url = new URL("http://www.RSS-Feed-URL-HERE");

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(false);
        XmlPullParser xpp = factory.newPullParser();

            // We will get the XML from an input stream
        xpp.setInput(getInputStream(url), "UTF_8");

            /* We will parse the XML content looking for the "<title>" tag which appears inside the "<item>" tag.
             * However, we should take in consideration that the rss feed name also is enclosed in a "<title>" tag.
             * As we know, every feed begins with these lines: "<channel><title>Feed_Name</title>...."
             * so we should skip the "<title>" tag which is a child of "<channel>" tag,
             * and take in consideration only "<title>" tag which is a child of "<item>"
             *
             * In order to achieve this, we will make use of a boolean variable.
             */
        boolean insideItem = false;

            // Returns the type of current event: START_TAG, END_TAG, etc..
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {

                if (xpp.getName().equalsIgnoreCase("item")) {
                    insideItem = true;
                } else if (xpp.getName().equalsIgnoreCase("title")) {
                    if (insideItem)
                        headlines.add(xpp.nextText()); //extract the headline
                } else if (xpp.getName().equalsIgnoreCase("link")) {
                    if (insideItem)
                        links.add(xpp.nextText()); //extract the link of article
                }
            }else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
                insideItem=false;
            }

            eventType = xpp.next(); //move to next element
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Binding data
    ArrayAdapter adapter = new ArrayAdapter(this,
            android.R.layout.simple_list_item_1, headlines);

    setListAdapter(adapter);


}
public InputStream getInputStream(URL url) {
       try {
           return url.openConnection().getInputStream();
       } catch (IOException e) {
           return null;
         }
    }

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
   Uri uri = Uri.parse((String) links.get(position));
   Intent intent = new Intent(Intent.ACTION_VIEW, uri);
   startActivity(intent);
}
//初始化实例变量
标题=新的ArrayList();
links=newarraylist();
试一试{
URL=新URL(“http://www.RSS-Feed-URL-HERE");
XmlPullParserFactory工厂=XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp=factory.newPullParser();
//我们将从输入流中获取XML
setInput(getInputStream(url),“UTF_8”);
/*我们将解析XML内容,查找出现在“”标记内的“”标记。
*但是,我们应该考虑到rss提要名称也包含在“”标记中。
*正如我们所知,每个提要都以以下行开头:“提要名称…”
*所以我们应该跳过“”标记,它是“”标记的子级,
*并且只考虑“”标记,它是“”的子级
*
*为了实现这一点,我们将使用布尔变量。
*/
布尔值insideItem=false;
//返回当前事件的类型:开始标记、结束标记等。。
int eventType=xpp.getEventType();
while(eventType!=XmlPullParser.END_文档){
if(eventType==XmlPullParser.START_标记){
if(xpp.getName().equalsIgnoreCase(“项”)){
insideItem=真;
}else if(xpp.getName().equalsIgnoreCase(“title”)){
如果(内部项目)
headlines.add(xpp.nextText());//提取标题
}else if(xpp.getName().equalsIgnoreCase(“链接”)){
如果(内部项目)
links.add(xpp.nextText());//提取文章的链接
}
}else if(eventType==XmlPullParser.END_标记&&xpp.getName().equalsIgnoreCase(“项”)){
insideItem=假;
}
eventType=xpp.next();//移动到下一个元素
}
}捕获(格式错误){
e、 printStackTrace();
}catch(XMLPullParseRexE){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
//绑定数据
ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_list_item_1,标题);
setListAdapter(适配器);
}
公共输入流getInputStream(URL){
试一试{
返回url.openConnection().getInputStream();
}捕获(IOE异常){
返回null;
}
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
Uri=Uri.parse((字符串)links.get(位置));
意图=新意图(Intent.ACTION\u视图,uri);
星触觉(意向);
}

&您还可以使类扩展活动,并手动将两个ListView放入活动中,然后将此代码处理为两个ListView。

我建议您先看看Android支持库中的ViewPager,然后看看这个非常棒的库:Thanx…我会尝试一下,让您知道:)