Question

I'm not sure why I'm getting the exception. All I'm trying to do is get the first description tag of the document (which should end up being "The best tracks of the week").

package com.example.rssparser;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.xml.sax.InputSource;
import org.xmlpull.v1.XmlPullParser;

import com.example.rssparser.R;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Xml;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         try{
            new Parse().execute(new URL("http://staging.satelliterecords.com/hot_picks/rss"));
         }catch(MalformedURLException e){
            Toast.makeText(this, "Error: Malformed URL", Toast.LENGTH_LONG).show();
         }
    }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.main, menu);
         return true;
     }
     public String parseInputSource(InputSource inputSource) throws Exception{  
         XmlPullParser parser = Xml.newPullParser();
         parser.setInput(inputSource.getCharacterStream());
         int eventType = parser.getEventType();
         String description = "";
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType){
            case XmlPullParser.START_DOCUMENT:
                break;
            case XmlPullParser.START_TAG:
                String tagName = parser.getName();
                if (tagName.equalsIgnoreCase("description")){
                    description = parser.nextText();
                    return description;
                }
                break;
            default:
                break;
            }
            eventType = parser.next();

        }
    throw new RuntimeException("No description tag found in XML document");
    }

    private class Parse extends AsyncTask<URL, Void, String>{

        protected String doInBackground(URL...url){
            try{
                InputSource inputSource = new InputSource(url[0].openStream());
                String result = parseInputSource(inputSource);
                return result;
            }catch(IOException e){
                return "Error IO";
            }catch(Exception e){
                return "Error PullParser";
            }
        }
        protected void onPostExecute(String result) {
            TextView txt = (TextView) findViewById(R.id.helloyo);
            txt.setText(result);
        }

    }

}

The text area is outputting "Error PullParser". I'm relatively new to Java and even newer to Android dev so my error handling skills probably aren't as good as they should be. Also I have no idea how to read the LogCat so I'm not sure if the answer is hidden in there.

Was it helpful?

Solution

Wow. So I was able to figure out how to make the exception send an error message and found it in the LogCat. For some reason it didn't recognize the "setInput" method. I changed the InputSource to an InputStream and changed the setInput method to parser.setInput(inputStream, "UTF-8"); which made it work.

What I'm confused about though is that the XMLPullParser API claims that the setInput method only takes one parameter (the stream). http://www.xmlpull.org/v1/doc/api/org/xmlpull/v1/XmlPullParser.html#setInput%28java.io.Reader%29

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top