2013-01-04 88 views
2

我遵循一個簡單的教程(http://www.cse.nd.edu/courses/cse40814/www/RSS_Android.pdf)從一個給定的URL讀取RSS提要到一個listView。我添加了INTERNET權限,Eclipse中的代碼無錯,但在設備或模擬器上啓動時不會顯示任何RSS提要。我無法讓代碼變得更簡單,而且我使用的Feed是來自www.nba.com的穩定Feed:http://www.nba.com/rss/nba_rss.xml,儘管我已經在幾個RSS Feed上進行了測試,但仍未顯示Feed。Android RSS閱讀器(忍者錯誤)

任何想法傢伙?

Main.java

package com.android.simplerssreader; 

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

import com.android.simplerssreader.data.RssItem; 
import com.android.simplerssreader.util.RssReader; 

public class Main extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    try { 

     RssReader rssReader = new RssReader(
       "http://www.nba.com/rss/nba_rss.xml"); 
     ListView Items = (ListView) findViewById(R.id.listView1); 

     ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(this, 
       android.R.layout.simple_list_item_1, rssReader.getItems()); 

     Items.setAdapter(adapter); 
     Items.setOnItemClickListener(new ListListener(rssReader.getItems(), 
       this)); 

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

} 
} 

ListListener.java

package com.android.simplerssreader; 

import java.util.List; 

import com.android.simplerssreader.data.RssItem; 

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; 

public class ListListener implements OnItemClickListener { 

List<RssItem> listItems; 
Activity activity; 

public ListListener(List<RssItem> listItems, Activity activity) { 
    this.listItems = listItems; 
    this.activity = activity; 
} 

public void onItemClick(AdapterView<?> parent, View view, int pos, long id) { 

    Intent i = new Intent(Intent.ACTION_VIEW); 
    i.setData(Uri.parse(listItems.get(pos).getLink())); 
    activity.startActivity(i); 

} 

} 

RssItem.java

package com.android.simplerssreader.data; 

public class RssItem { 
private String title; 
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; 
} 

} 

RssParseHandler.java

package com.android.simplerssreader.util; 

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.android.simplerssreader.data.RssItem; 

public class RssParseHandler extends DefaultHandler { 

private List<RssItem> rssItems; 

private RssItem currentItem; 
private boolean parsingTitle; 
private boolean parsingLink; 

public RssParseHandler() { 
    rssItems = new ArrayList<RssItem>(); 
} 

public List<RssItem> getItems() { 
    return rssItems; 
} 

@Override 
public void startElement(String uri, String localName, String qName, 
     Attributes attributes) throws SAXException { 
    if ("content-item".equals(qName)) { 
     currentItem = new RssItem(); 
    } else if ("title".equals(qName)) { 
     parsingTitle = true; 
    } else if ("url".equals(qName)) { 
     parsingLink = true; 
    } 
} 

@Override 
public void endElement(String uri, String localName, String qName) 
     throws SAXException { 
    if ("content-item".equals(qName)) { 
     rssItems.add(currentItem); 
     currentItem = null; 
    } else if ("title".equals(qName)) { 
     parsingTitle = false; 
    } else if ("url".equals(qName)) { 
     parsingLink = false; 
    } 
} 

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

} 

RssReader.java

package com.android.simplerssreader.util; 

import java.util.List; 

import javax.xml.parsers.SAXParser; 
import javax.xml.parsers.SAXParserFactory; 

import com.android.simplerssreader.data.RssItem; 

public class RssReader { 

private String rssUrl; 

public RssReader(String rssUrl) { 

    this.rssUrl = rssUrl; 
} 

public List<RssItem> getItems() throws Exception { 
    SAXParserFactory factory = SAXParserFactory.newInstance(); 
    SAXParser saxParser = factory.newSAXParser(); 

    RssParseHandler handler = new RssParseHandler(); 
    saxParser.parse(rssUrl, handler); 
    return handler.getItems(); 
} 
} 

main.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:background="#000000" 
android:orientation="vertical" > 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#000000" 
    android:cacheColorHint="#FFA500" 
    android:scrollingCache="false" 
    android:textColor="#ADD8E6" > 
</ListView> 

</LinearLayout> 

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.android.simplerssreader" 
android:versionCode="1" 
android:versionName="1.0" > 

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

<uses-sdk 
    android:minSdkVersion="7" 
    android:targetSdkVersion="16" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.android.simplerssreader.Main" 
     android:label="@string/title_activity_main" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 

logcat的

01-04 16:22:16.171:E/SimpleRssReader(8685):無法打開http://www.nba.com/rss/nba_rss.xml

回答

1

取代

Log.e("SimpleRssReader", e.getMessage()); 

Log.e("SimpleRssReader", e.getMessage(), e); 

你失去了堆棧跟蹤信息。

順便說一句,你的錯誤是,你不能訪問Android的網絡時,你是在UI線程(蜂窩後)內:How to fix android.os.NetworkOnMainThreadException?

+0

Snicolas我想要說謝謝你借給你的專業知識開始。我改變了這一行,我開始得到正確的錯誤:「android.os.NetworkOnMainThreadException」。我看了一下你提供的鏈接,但在給它看後,我不完全確定如何在我的Main.java中實現這個。我知道我可以在我的嘗試塊子,但是當我擴展AsyncTask我需要什麼參數?而且我並不需要聲明「private exception exception」我沒有這個方法「new RetreiveFeedTask()。execute(urlToRssFeed);」對?你能告訴我如何在我的Main.java中實現這個嗎? – AndroidNewb

+0

這實際上是Android上的一個長話題。首先嚐試使用AsyncTask並使您的代碼正常工作。在這種情況下,定義一個簡單的AsyncTask >,你所要做的就是在你的asynctask的doInBackGround方法內調用你的RssReader,並在你的asynctask上調用execute來傳遞url。請花點時間,尋找幾個例子,你會做到的。但是,你應該記住,這種技術有一些缺點,當你用android更舒適時,看一看robospice,它是github上的一個lib,它提供了一個更強大的網絡連接方法。 – Snicolas

+0

並接受這個答案,如果它符合您的需求。 – Snicolas