1

I have been working on a RSS Reader app. Till now I have learned to parse xml data from web and display needed data. I used Busy Coder's Guide and tutorials from Derek Banas to study this topic and built a Stock Quote reader.

Anyways, I wanted to create an RSS reader to read feed from some news website. The problem being (as far as I have understood), that when I click the RSS link from this page, the webpage renders the xml itself (please correct me if I am wrong at this point). So I right clicked and used "View Source" to get the xml from source code

Now after editing some code and trying with the links from the tutorial to confirm that my program runs right, I used the new view source url but the app stopped working.

Through the try-catch block in doInBackground I get the exception - "java.net.MalformedURLException: Unknown protocol: view=source".

So, is there a way to read the handle this exception and make the parser read the xml file? Thanks, This is my code from CricketActivity. It is called when a button is pressed in MainActivity.

package com.example.try_rssfeed;


public class CricketActivity extends ListActivity {



private static final String TAG = "CRICKET"; // For Logcat

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.d(TAG, "onCreate");

    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>()));


    final String cricketURL = "view-source:http://timesofindia.feedsportal.com/c/33039/f/533920/index.rss";

    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        new MyAsyncTask().execute(cricketURL);
    } else {
        Log.d(TAG, "No network connection available.");
    }

}

private class MyAsyncTask extends AsyncTask<String, String, Void> {
@Override
    protected Void doInBackground(String... args) {
        Log.d(TAG, "In doBackground ");
        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();

            InputStream streamUrl = loadXmlFromNetwork(args[0]);
            Log.d(TAG, "stream " + streamUrl.toString());
            xpp.setInput(streamUrl, null);

            Log.d(TAG, "After setInput " + xpp.getEventType());

            int count = 1; // For Logcat

            do {
                Log.d(TAG, "in While " + count);
                if (xpp.getEventType() == XmlPullParser.START_TAG && xpp.getName().equals("title")) {
                    xpp.next();
                    Log.d(TAG, xpp.getEventType() + "____" + count++);
                    publishProgress(xpp.getText());
                }
                xpp.next();
            } while (xpp.getEventType() != XmlPullParser.END_DOCUMENT);

        } catch (Throwable t) {
            Log.d(TAG, t.toString());               // <-------------- THIS THROWS EXCEPTION
        }
        return null;
    }

    private InputStream loadXmlFromNetwork(String urlString) throws XmlPullParserException, IOException {
        Log.d(TAG, "LoadXML");

        InputStream stream = null;
        try {
            Log.d(TAG, "LoadXML Try " + urlString);

            URL url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            // Starts the query
            conn.connect();
            stream = conn.getInputStream();

            // Log.d(TAG, "stream "+stream.toString());
        } finally {
        }

        return stream;

    }

    protected void onProgressUpdate(String... values) {
        Log.d(TAG, "In ProgressUpdate");
        ((ArrayAdapter<String>) getListAdapter()).add(values[0]);
    }

    @Override
    protected void onPostExecute(Void result) {
        Log.d(TAG, "===================");
        Log.d(TAG, "DONE DONE DONE DONE");
    }
}

}

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
Sahil Dave
  • 433
  • 1
  • 7
  • 15

0 Answers0