我一直在关注一个向我展示如何创建rssfeed阅读器的教程,我来到教程的最后;并且Feed未显示在listView中。所以我在寻找logCat中的错误,但我能找到的唯一一个只是说'#null;'这根本没用。
有人能发现我写的代码存在潜在问题吗?
感谢。
DirectRSS(主要类):
package com.example.rssapplication;
import java.util.List;
import android.app.ListActivity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class DirectRSS extends ListActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.directrss);
//Set to portrait, so that every time the view changes; it does not run the DB query again...
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
try{
RssReader1 rssReader = new RssReader1("http://www.skysports.com/rss/0,20514,11661,00.xml");
ListView list = (ListView)findViewById(R.id.list);
ArrayAdapter<RssItem1> adapter = new ArrayAdapter<RssItem1>(this, android.R.layout.simple_list_item_1);
list.setAdapter(adapter);
list.setOnItemClickListener(new ListListener1(rssReader.getItems(),this));
}catch(Exception e)
{
String err = (e.getMessage()==null)?"SD Card failed": e.getMessage();
Log.e("sdcard-err2:",err + " " + e.getMessage());
// Log.e("Error", e.getMessage());
Log.e("LOGCAT", "" + e.getMessage());
}
}
}
ListListener1:
package com.example.rssapplication;
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;
public class ListListener1 implements OnItemClickListener{
List<RssItem1> listItems;
Activity activity;
public ListListener1(List<RssItem1> listItems, Activity activity)
{
this.listItems = listItems;
this.activity = activity;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
// TODO Auto-generated method stub
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(listItems.get(pos).getLink()));
activity.startActivity(i);
}
}
RssItem1:
package com.example.rssapplication;
public class RssItem1 {
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;
}
}
RssParseHandler1:
package com.example.rssapplication;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class RssParseHandler1 extends DefaultHandler{
private List<RssItem1> rssItems;
private RssItem1 currentItem;
private boolean parsingTitle;
private boolean parsingLink;
public RssParseHandler1(){
rssItems = new ArrayList<RssItem1>();
}
public List<RssItem1> getItems(){
return rssItems;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if("item".equals(qName)){
currentItem = new RssItem1();
}
else if("title".equals(qName)){
parsingTitle = true;
}
else if("link".equals(qName)){
parsingLink = true;
}
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
}
@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;
}
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
}
@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;
}
}
// TODO Auto-generated method stub
super.characters(ch, start, length);
}
}
RssReader1:
package com.example.rssapplication;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class RssReader1 {
private String rssUrl;
public RssReader1(String rssUrl)
{
this.rssUrl = rssUrl;
}
public List<RssItem1> getItems() throws Exception
{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
RssParseHandler1 handler = new RssParseHandler1();
saxParser.parse(rssUrl, handler);
return handler.getItems();
}
}
这也是logCat:
08-25 11:13:20.803: D/AbsListView(26291): unregisterIRListener() is called
08-25 11:13:20.803: D/AbsListView(26291): unregisterIRListener() is called
08-25 11:13:20.803: D/AbsListView(26291): unregisterIRListener() is called
08-25 11:13:20.813: D/AbsListView(26291): unregisterIRListener() is called
08-25 11:13:20.813: D/AbsListView(26291): unregisterIRListener() is called
08-25 11:13:20.813: D/AbsListView(26291): unregisterIRListener() is called
08-25 11:13:20.813: W/ApplicationPackageManager(26291): getCSCPackageItemText()
08-25 11:13:20.843: D/AbsListView(26291): Get MotionRecognitionManager
08-25 11:13:20.843: E/sdcard-err2:(26291): SD Card failed null
08-25 11:13:20.843: E/LOGCAT(26291): null
08-25 11:13:20.843: D/AbsListView(26291): onVisibilityChanged() is called, visibility : 4
08-25 11:13:20.843: D/AbsListView(26291): unregisterIRListener() is called
08-25 11:13:20.873: D/AbsListView(26291): onVisibilityChanged() is called, visibility : 0
08-25 11:13:20.883: D/AbsListView(26291): unregisterIRListener() is called
08-25 11:13:20.903: D/AbsListView(26291): unregisterIRListener() is called
08-25 11:13:20.933: D/AbsListView(26291): unregisterIRListener() is called
08-25 11:13:20.963: D/AbsListView(26291): unregisterIRListener() is called
08-25 11:13:20.973: D/AbsListView(26291): unregisterIRListener() is called
08-25 11:13:21.323: D/AbsListView(26291): onVisibilityChanged() is called, visibility : 4
08-25 11:13:21.323: D/AbsListView(26291): unregisterIRListener() is called
08-25 11:13:21.323: D/AbsListView(26291): onVisibilityChanged() is called, visibility : 4
08-25 11:13:21.323: D/AbsListView(26291): unregisterIRListener() is called
08-25 11:13:21.323: D/AbsListView(26291): onVisibilityChanged() is called, visibility : 4
08-25 11:13:21.323: D/AbsListView(26291): unregisterIRListener() is called
08-25 11:13:21.323: D/AbsListView(26291): onVisibilityChanged() is called, visibility : 4
08-25 11:13:21.323: D/AbsListView(26291): unregisterIRListener() is called
08-25 11:13:21.323: D/AbsListView(26291): onVisibilityChanged() is called, visibility : 4
08-25 11:13:21.323: D/AbsListView(26291): unregisterIRListener() is called
08-25 11:13:21.333: D/AbsListView(26291): onVisibilityChanged() is called, visibility : 4
08-25 11:13:21.333: D/AbsListView(26291): unregisterIRListener() is called
新日志:
08
-25 11:33:52.234: W/System.err(31673): java.io.IOException: Couldn't open http://www.skysports.com/rss/0,20514,11661,00.xml
08-25 11:33:52.244: W/System.err(31673): at org.apache.harmony.xml.ExpatParser.openUrl(ExpatParser.java:755)
08-25 11:33:52.244: W/System.err(31673): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:292)
08-25 11:33:52.244: W/System.err(31673): at javax.xml.parsers.SAXParser.parse(SAXParser.java:390)
08-25 11:33:52.244: W/System.err(31673): at javax.xml.parsers.SAXParser.parse(SAXParser.java:266)
08-25 11:33:52.244: W/System.err(31673): at com.example.rssapplication.RssReader1.getItems(RssReader1.java:27)
08-25 11:33:52.244: W/System.err(31673): at com.example.rssapplication.DirectRSS.onCreate(DirectRSS.java:27)
08-25 11:33:52.244: W/System.err(31673): at android.app.Activity.performCreate(Activity.java:5426)
08-25 11:33:52.244: W/System.err(31673): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
08-25 11:33:52.244: W/System.err(31673): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
08-25 11:33:52.244: W/System.err(31673): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
08-25 11:33:52.244: W/System.err(31673): at android.app.ActivityThread.access$900(ActivityThread.java:161)
08-25 11:33:52.244: W/System.err(31673): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
08-25 11:33:52.244: W/System.err(31673): at android.os.Handler.dispatchMessage(Handler.java:102)
08-25 11:33:52.244: W/System.err(31673): at android.os.Looper.loop(Looper.java:157)
08-25 11:33:52.244: W/System.err(31673): at android.app.ActivityThread.main(ActivityThread.java:5356)
08-25 11:33:52.244: W/System.err(31673): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 11:33:52.244: W/System.err(31673): at java.lang.reflect.Method.invoke(Method.java:515)
08-25 11:33:52.244: W/System.err(31673): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
08-25 11:33:52.244: W/System.err(31673): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
08-25 11:33:52.244: W/System.err(31673): at dalvik.system.NativeStart.main(Native Method)
08-25 11:33:52.244: W/System.err(31673): Caused by: android.os.NetworkOnMainThreadException
08-25 11:33:52.244: W/System.err(31673): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1156)
08-25 11:33:52.244: W/System.err(31673): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
08-25 11:33:52.244: W/System.err(31673): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
08-25 11:33:52.244: W/System.err(31673): at java.net.InetAddress.getAllByName(InetAddress.java:214)
08-25 11:33:52.244: W/System.err(31673): at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
08-25 11:33:52.244: W/System.err(31673): at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
08-25 11:33:52.244: W/System.err(31673): at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
08-25 11:33:52.244: W/System.err(31673): at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
08-25 11:33:52.244: W/System.err(31673): at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
08-25 11:33:52.244: W/System.err(31673): at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
08-25 11:33:52.244: W/System.err(31673): at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
08-25 11:33:52.244: W/System.err(31673): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
08-25 11:33:52.244: W/System.err(31673): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
08-25 11:33:52.244: W/System.err(31673): at org.apache.harmony.xml.ExpatParser.openUrl(ExpatParser.java:753)
08-25 11:33:52.244: W/System.err(31673): ... 19 more
08-25 11:33:52.244: E/LOGCAT(31673): Couldn't open http://www.skysports.com/rss/0,20514,11661,00.xml
答案 0 :(得分:0)
这将是这样的: 公共类DirectRSS扩展了ListActivity {
private AsyncTask<Void,Void,List<RssReader1>> test = new AsyncTask<Void, Void, List<RssReader1>>() {
@Override
protected List<RssReader1> doInBackground(Void... params) {
try{
RssReader1 rssReader = new RssReader1("http://www.skysports.com/rss/0,20514,11661,00.xml");
return rssReader.getItems();
} catch(Exception e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<RssReader1> strings) {
list.setOnItemClickListener(new ListListener1(strings,DirectRSS.this));
adapter.clear();
for (RssReader1 rr:Strings) {
adapter.add(rr);
}
adapter.notifyDatasetChanged();
super.onPostExecute(strings);
}
};
ArrayAdapter<RssItem1> adapter;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.directrss);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
list = (ListView)findViewById(R.id.list);
adapter = new ArrayAdapter<RssItem1>(this, android.R.layout.simple_list_item_1, new ArrayList<RssItem1>());
list.setAdapter(adapter);
test.execute();
}
}