-5

I make simply rss reader in android. I have the errors (I searched and tried some solutions but nothing):

Couldn't open http://pogoda.wp.pl/rss.xml
java.io.IOException: Couldn't open http://pogoda.wp.pl/rss.xml
at org.apache.harmony.xml.ExpatParser.openUrl(ExpatParser.java:755)
at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:292)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:390)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:266)
at com.newsreader.RssReader.getItems(RssReader.java:23)
at com.newsreader.MainActivity$1$1.run(MainActivity.java:48)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
at java.net.InetAddress.getAllByName(InetAddress.java:220)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
at org.apache.harmony.xml.ExpatParser.openUrl(ExpatParser.java:753)

and activity:

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new Thread(){
        public void run(){
            MainActivity.this.runOnUiThread(new Runnable(){

                @Override
                public void run() {
                    try{
                    RssReader reader = new RssReader("http://pogoda.wp.pl/rss.xml");
                    ListView items = (ListView) findViewById(R.id.listView1);
                    ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(MainActivity.this, android.R.layout.simple_list_item_1, reader.getItems());
                    items.setAdapter(adapter);
                    Log.i("", ""+reader);
                    items.setOnItemClickListener(new ListListener(reader.getItems(), MainActivity.this));
                    }catch(Exception e){
                        Log.e("blad", e.getMessage(), e);
                    }

                }

        });
        }
    }.start();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);

    return true;
}

 }

I don't know what am I doing wrong. Maybe someone has got the same problem so please help. Thanks

user978758
  • 609
  • 3
  • 17
  • 29

3 Answers3

2

It is because you are performing network operation on main thread of activity. Using Async task will be a better option.

DroidDev
  • 1,527
  • 4
  • 20
  • 42
1

Caused by: android.os.NetworkOnMainThreadException

This speaks for itself.

Don't do networking on the main Thread!

AsyncTask to the rescue. Try it out it will work.

Adam Arold
  • 29,285
  • 22
  • 112
  • 207
-2

Always use AsyncTask to solve this issue.

BUT, if you're within the movie Swordfish, someone points a gun at you and say "fix it in 30 seconds", then change your targetSdkVersion to 9 in the Android Manifest and the exception will stop. Don't worry, it will run on devices with version higher than 9 too. Even so, correct your code later using AsyncTask.

Italo Borssatto
  • 15,044
  • 7
  • 62
  • 88
  • don't you think changing target sdk version to 9 will restrict the app to only around 25% of android devices? – DroidDev Dec 05 '13 at 13:23
  • @VishwasSharma `targetSdkVersion` doesn't restrict the app to only version 9. It only shows that the app was explicitly tested to that version. But this solution is only for someone that really needs to correct the problem as fast as possible. – Italo Borssatto Dec 05 '13 at 13:25
  • it'll eliminate the exception, but will it run on devices having sdk version higher than 9? – DroidDev Dec 05 '13 at 13:28
  • @VishwasSharma Yes, exactly. It'll run on devices with sdk version higher than 9, for sure. – Italo Borssatto Dec 05 '13 at 13:30
  • ok, never tested it that way – DroidDev Dec 05 '13 at 13:36
  • @VishwasSharma I don't use it anymore, but was very usefull one time, when the app was already deployed and we changed the `targetSdkVersion` just before the deploy. – Italo Borssatto Dec 05 '13 at 13:37
  • 1
    refactoring the code to use AsyncTask might take 5 minutes. So unless you are within the movie Swordfish where someone points a gun at you and say: "fix it in 30 seconds", there will never be an emergency big enough to just change the targetSdkVersion... also you will get the ["Menu button of shame!"](https://www.google.de/search?q=menu+button+of+shame) – WarrenFaith Dec 05 '13 at 14:16
  • 1
    I see what you did there :) – WarrenFaith Dec 05 '13 at 15:51