Andrew-Saarima
Andrew-Saarima

Reputation: 270

Java program works, but won't run in Android Studio

So for a class I have to work with an RSS reader program. The code was basically given to us but we have to put it into android. The code works in Java, but when I try to run it in Android modifying it so that it will change a TextViews text, it compiles but at run-time I get an avalanche of errors that I don't understand. Here's my code. I assume it has something to do with my use of setText() or the TextView object.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    TextView rssFeed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rssFeed = (TextView)findViewById(R.id.txtFeed);
        rssFeed.setText(readRSS("http://rss.cnn.com/rss/edition.rss"));
    }

    public static String readRSS(String urlAddress)
    {
        try{
            URL rssUrl = new URL(urlAddress);
            BufferedReader in = new BufferedReader(new InputStreamReader(rssUrl.openStream()));
            String sourceCode = "";
            String line;
            while ((line = in.readLine()) != null) {
                int titleEndIndex = 0;
                int titleStartIndex = 0;
                while (titleStartIndex >= 0) {
                    titleStartIndex = line.indexOf("<title>", titleEndIndex);
                    if (titleStartIndex >= 0) {
                        titleEndIndex = line.indexOf("</title>", titleStartIndex);
                        sourceCode += line.substring(titleStartIndex + "<title>".length(), titleEndIndex) + "\n";
                    }
                }
            }
            in.close();
            return sourceCode;
        }
        catch (MalformedURLException ue) {
            System.out.print("Malformed URL");
        }
        catch (IOException ioe)
        {
            System.out.println("Something went wrong reading the contents");
        }
        return "Program Failed";
    }

The XML is

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtFeed" />
</RelativeLayout>

The logcat is

10-05 23:05:49.839 2087-2087/com.example.andrewsaarima.rssreader I/art: Not late-enabling -Xcheck:jni (already on)
10-05 23:05:49.840 2087-2087/com.example.andrewsaarima.rssreader I/art: Late-enabling JIT
10-05 23:05:49.843 2087-2087/com.example.andrewsaarima.rssreader I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
10-05 23:05:49.996 2087-2087/com.example.andrewsaarima.rssreader W/System: ClassLoader referenced unknown path: /data/app/com.example.andrewsaarima.rssreader-2/lib/x86
10-05 23:05:50.220 2087-2087/com.example.andrewsaarima.rssreader D/AndroidRuntime: Shutting down VM
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: FATAL EXCEPTION: main
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: Process: com.example.andrewsaarima.rssreader, PID: 2087
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.andrewsaarima.rssreader/com.example.andrewsaarima.rssreader.MainActivity}: android.os.NetworkOnMainThreadException
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:  Caused by: android.os.NetworkOnMainThreadException
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at java.net.InetAddress.getAllByName(InetAddress.java:215)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:231)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at java.net.URL.openStream(URL.java:470)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at com.example.andrewsaarima.rssreader.MainActivity.readRSS(MainActivity.java:28)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at com.example.andrewsaarima.rssreader.MainActivity.onCreate(MainActivity.java:21)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6237)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417) 
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
10-05 23:05:53.950 2087-2094/com.example.andrewsaarima.rssreader W/art: Suspending all threads took: 25.938ms
10-05 23:05:55.994 2087-2087/? I/Process: Sending signal. PID: 2087 SIG: 9

Upvotes: 3

Views: 321

Answers (2)

Eli Blokh
Eli Blokh

Reputation: 12293

NetworkOnMainThreadException means that you should not to perform web requests in application main thread. Instead use Loaders (it's preferably than AsyncTask, read here why) or some library for networking (for example Volley or Retrofit).

After you get and parse response you could pass result String into TextView

Upvotes: 2

Zubair Ahmed
Zubair Ahmed

Reputation: 2897

You should use AsyncTask in android in order to call for rss feed url. Its very simple to implement. First thing you should do is to read about AsyncTask from here if you don't know about it. Then implement your code according to AsyncTask like:

    private class RSSTask extends AsyncTask<String, Void, String>
    {
        ProgressDialog pd;

        //onPreExecute() will be called before entering into background thread
        @Override
        protected void onPreExecute() 
        {
            super.onPreExecute();

            pd = ProgressDialog.show(context, "RSS Feed", "Fetching Rss Feed Please Wait");

        }

        //this is background thread called after onPreExecute()
        @Override
        protected String doInBackground(String... params) 
        {
            String rssResponse = readRSS(params[0])

            return rssResponse;
        }

        //called after doInBackground() finished
        @Override
        protected void onPostExecute(String result) 
        {
            super.onPostExecute(result);

            pd.dismiss();

            rssFeed.setText(result);// here result will be returning string from doInBackground()
        }
    }

Copy this whole AsyncTask class to your MainActivity and then

Replace:

rssFeed.setText(readRSS("http://rss.cnn.com/rss/edition.rss"));

With

RSSTask rssT = new RSSTask(); rssT.execute("http://rss.cnn.com/rss/edition.rss");

Hope you would get desired output.

Upvotes: 4

Related Questions