试图从crickInfo网站上获取来自Rss feed的数据,但没有运气

时间:2013-06-22 15:13:06

标签: android xml rss

我正在尝试开发一个可以显示http://www.espncricinfo.com/分数的应用程序但是没有用,我无法做到。我跟着很多例子,但他们没有帮助我。链接到RSS Feed http://static.cricinfo.com/rss/livescores.xml

<rss version="2.0">
<channel>
<title>Cricinfo Live Scores</title>
<ttl>2</ttl>
<link>http://www.cricinfo.com</link>
<description>Latest scores from Cricinfo</description>
<copyright>(c)Cricinfo</copyright>
<language>en-gb</language>
<pubDate>Sat, 22 Jun 2013 15:10:04 +0000</pubDate>
<item>
<title>Worcestershire 505/7 v Glamorgan 69/3 & 277/10 *</title>
<link>
http://www.cricinfo.com/ci/engine/match/593568.html?CMP=OTC-RSS
</link>
<description>Worcestershire 505/7 v Glamorgan 69/3 & 277/10 *</description>
<guid>
http://www.cricinfo.com/ci/engine/match/593568.html
</guid>
</item>
.......
......
</channel>

我只是想在Title标签之间获取Info,但我无法做到。这是我的java代码如下:

public class MainActivity extends Activity 
{
//private final static String TAG = XMLResourceDemo.class.getSimpleName();
TextView selection;
ArrayList<String> items_title = new ArrayList<String>();
ListView ListView1 = null;
private ArrayAdapter arrayAdapter;
//List<String> title = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //selection = (TextView) findViewById(R.id.selection);
    ListView1 = (ListView) findViewById(R.id.ListView1);

    /** Create a new layout to display the view */
   // LinearLayout layout = new LinearLayout(this);
  //  layout.setOrientation(1);

    /** Create a new textview array to display the results */

    try 
    {
        URL url = new URL("http://static.cricinfo.com/rss/livescores.xml");
        URLConnection conn = url.openConnection();

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(conn.getInputStream());

        NodeList nodes = doc.getElementsByTagName("item");
        for (int i = 0; i < nodes.getLength(); i++)
        {
            Element element = (Element) nodes.item(i);
            NodeList title = element.getElementsByTagName("title");
            Element line = (Element) title.item(0);
            items_title.add(line.getTextContent());
        }
        arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items_title);
        ListView1.setAdapter(arrayAdapter);
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

我的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" >


<ListView
    android:id="@+id/ListView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
</ListView>

和我的andriod manifest权限如下:

<uses-permission android:name="android.permission.INTERNET"/>

我的问题:我可以在没有任何错误的情况下运行我的应用程序,但它只显示空白页。

0 个答案:

没有答案