0

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.

4

1 に答える 1

0

わお。そのため、例外にエラーメッセージを送信させる方法を理解することができ、LogCat でそれを見つけました。何らかの理由で「setInput」メソッドを認識しませんでした。InputSource を InputStream に変更し、それを機能させる setInput メソッドを変更しましたparser.setInput(inputStream, "UTF-8");

私が混乱しているのは、XMLPullParser API が、setInput メソッドが 1 つのパラメーター (ストリーム) しかとらないと主張していることです。http://www.xmlpull.org/v1/doc/api/org/xmlpull/v1/XmlPullParser.html#setInput%28java.io.Reader%29

于 2013-07-09T18:07:57.487 に答える