0

I am trying to read RSS feeds from a website and display them on my app in android. However, I am running into errors. Any ideas? i will post my logcat after the code. Please help.

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
JD.
  • 3,005
  • 2
  • 26
  • 37
Nidhin_toms
  • 707
  • 4
  • 18
  • 29
  • You should really try to research your own problems. Instead of expecting to be spoonfed the answer on stackoverflow. Learn to analyze your logs, this is a very important skill. – JoxTraex Aug 02 '12 at 22:17
  • the problem was that my android was running on 4.0 . I wrote the same code on android 1.5 and it worked properly. – Nidhin_toms Aug 02 '12 at 22:39
  • @Nidhin_toms starting with 3.0 making network calls on the main thread will raise an exception. Although changing your API level to a lower one will technically "fix" this, the correct way to do is use Thread/Handler or AsyncTask to move your network operation off of the main thread. Which will also result in a better user experience, and eliminate the chance of ANR force close. – FoamyGuy Aug 02 '12 at 22:46

1 Answers1

0
Caused by: android.os.NetworkOnMainThreadException

That line in your stacktrace is telling you that you are trying to make a network call (fetch the RSS) from the main thread. Starting with API 11 (? I think) this will raise an exception.

It is the platform designers way of trying to tell you that you really should do your network operations on the background thread.

To fix this you'll need to move your network calls off of the main thread. You can use either Thread / Handler, or AsyncTask to achieve this

This tutorial by Lars Vogel is a FANTASTIC overview of both

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156