适用于Android的RSS阅读器问题

时间:2012-08-02 22:09:28

标签: android rss

我正在尝试从网站上阅读RSS源并在我的Android应用程序中显示它们。但是,我遇到了错误。有任何想法吗?我将在代码后发布我的logcat。请帮忙。

package nidhin.rss;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class RssActivity extends Activity {
/** Called when the activity is first created. */


Button ButtonStats ;
List headlines;

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ButtonStats = (Button) findViewById(R.id.ButtonStats);
    ButtonStats.setOnClickListener(new clicker());
    headlines = new ArrayList();

    try {


    URL url = new URL("http://feeds.pcworld.com/pcworld/latestnews");

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

    xpp.setInput(getInputStream(url), "UTF_8");

    boolean insideItem = false;
    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(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();

        }






}

public InputStream getInputStream(URL url) {

       try {

           return url.openConnection().getInputStream();

       } catch (IOException e) {

           return null;

         }

    }



class clicker implements Button.OnClickListener
{
        public void onClick(View v)
        {                        
            if(v== ButtonStats)
            {


            }
    }

   }
}

logcat的:

08-02 21:40:02.320: E/AndroidRuntime(723): FATAL EXCEPTION: main
08-02 21:40:02.320: E/AndroidRuntime(723): java.lang.RuntimeException: Unable to start activity ComponentInfo{nidhin.rss/nidhin.rss.RssActivity}: android.os.NetworkOnMainThreadException
08-02 21:40:02.320: E/AndroidRuntime(723):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-02 21:40:02.320: E/AndroidRuntime(723):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-02 21:40:02.320: E/AndroidRuntime(723):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-02 21:40:02.320: E/AndroidRuntime(723):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-02 21:40:02.320: E/AndroidRuntime(723):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 21:40:02.320: E/AndroidRuntime(723):  at android.os.Looper.loop(Looper.java:137)
08-02 21:40:02.320: E/AndroidRuntime(723):  at android.app.ActivityThread.main(ActivityThread.java:4424)
08-02 21:40:02.320: E/AndroidRuntime(723):  at java.lang.reflect.Method.invokeNative(Native Method)
08-02 21:40:02.320: E/AndroidRuntime(723):  at java.lang.reflect.Method.invoke(Method.java:511)
08-02 21:40:02.320: E/AndroidRuntime(723):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-02 21:40:02.320: E/AndroidRuntime(723):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-02 21:40:02.320: E/AndroidRuntime(723):  at dalvik.system.NativeStart.main(Native Method)
08-02 21:40:02.320: E/AndroidRuntime(723): Caused by: android.os.NetworkOnMainThreadException
08-02 21:40:02.320: E/AndroidRuntime(723):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
08-02 21:40:02.320: E/AndroidRuntime(723):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
08-02 21:40:02.320: E/AndroidRuntime(723):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
08-02 21:40:02.320: E/AndroidRuntime(723):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
08-02 21:40:02.320: E/AndroidRuntime(723):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
08-02 21:40:02.320: E/AndroidRuntime(723):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
08-02 21:40:02.320: E/AndroidRuntime(723):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
08-02 21:40:02.320: E/AndroidRuntime(723):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
08-02 21:40:02.320: E/AndroidRuntime(723):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
08-02 21:40:02.320: E/AndroidRuntime(723):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
08-02 21:40:02.320: E/AndroidRuntime(723):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
08-02 21:40:02.320: E/AndroidRuntime(723):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
08-02 21:40:02.320: E/AndroidRuntime(723):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
08-02 21:40:02.320: E/AndroidRuntime(723):  at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
08-02 21:40:02.320: E/AndroidRuntime(723):  at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
08-02 21:40:02.320: E/AndroidRuntime(723):  at nidhin.rss.RssActivity.getInputStream(RssActivity.java:107)
08-02 21:40:02.320: E/AndroidRuntime(723):  at nidhin.rss.RssActivity.onCreate(RssActivity.java:47)
08-02 21:40:02.320: E/AndroidRuntime(723):  at android.app.Activity.performCreate(Activity.java:4465)
08-02 21:40:02.320: E/AndroidRuntime(723):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-02 21:40:02.320: E/AndroidRuntime(723):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-02 21:40:02.320: E/AndroidRuntime(723):  ... 11 more

1 个答案:

答案 0 :(得分:0)

Caused by: android.os.NetworkOnMainThreadException

您的stacktrace中的那一行告诉您正在尝试从主线程进行网络调用(获取RSS)。从API 11开始(?我认为)这将引发异常。

平台设计者试图告诉您,您确实应该在后台线程上进行网络操作。

要解决此问题,您需要将网络呼叫从主线程移开。您可以使用Thread / HandlerAsyncTask来实现此目标

This tutorial by Lars Vogel is a FANTASTIC overview of both