0

我正在尝试为我的博客制作一个 RSSReader。我做了一个自己的处理程序。但是当我尝试解析它时它不起作用

这是我的处理程序

import java.util.jar.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class myHandler extends DefaultHandler {

    final private String ENTRY = new String("entry");

    final private String TITLE = new String("title");

    final private String LINK = new String("link");

    final private String LINK_ATTR_HREF = new String("href");

    private boolean entryBoolean;

    private boolean titleBoolean;

    private String contentTitle;

    private Content myContent;

    public myHandler(Content content) {
        myContent = content;
    }

    @Override
    public void startDocument() throws SAXException {

        entryBoolean = false;
        titleBoolean = false;
        contentTitle = "";
    }

    @Override
    public void endDocument() throws SAXException {

    }

    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {

        if (localName.equalsIgnoreCase(this.ENTRY)) {
            this.entryBoolean = true;
        }
        else if (localName.equalsIgnoreCase(this.TITLE)) {
            this.titleBoolean = true;
        }
        else if (localName.equalsIgnoreCase(this.LINK)) {

            if (entryBoolean) {

                Log.i(myHandler.class.getSimpleName(), atts.getValue(this.LINK_ATTR_HREF));

                myContent.setUrl(atts.getValue(this.LINK_ATTR_HREF));
            }
        }
    }

    @Override
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {

        if (localName.equalsIgnoreCase(this.ENTRY)) {
            this.entryBoolean = false;
        }
        else if (localName.equalsIgnoreCase(this.TITLE)) {
            this.titleBoolean = false;

            if (this.entryBoolean) {

                Log.i(myHandler.class.getSimpleName(), this.contentTitle);

                myContent.setTitel(this.contentTitle);

                this.contentTitle = "";
            }
        }

    }

    @Override
    public void characters(char ch[], int start, int length) throws SAXException {

        String textBetween = new String(ch, start, length);

        if (this.entryBoolean) {
            // nur Title innerhalb des Entry Element werden ausgelesen
            if (this.titleBoolean) {
                this.contentTitle = this.contentTitle + textBetween;
            }

        }
    }
}



@Override
public void endElement(String namespaceURI, String localName, String qName)throws SAXException {

    if(localName.equalsIgnoreCase(this.ENTRY)){
        this.entryBoolean = false;
    }
    else if(localName.equalsIgnoreCase(this.TITLE)){
        this.titleBoolean = false;

        if(this.entryBoolean){

            Log.i(myHandler.class.getSimpleName(), this.contentTitle);

            myContent.setTitel(this.contentTitle);


            this.contentTitle = "";
        }
    }

}


@Override
public void characters(char ch[], int start, int length)throws SAXException {

    String textBetween = new String(ch, start, length);

    if(this.entryBoolean){
        //nur Title innerhalb des Entry Element werden ausgelesen
        if(this.titleBoolean){
            this.contentTitle = this.contentTitle + textBetween;
         }

        }
    }}

这是我的 RSSMain.java

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class RSSMain extends Activity {

    private final String tag = RSSMain.class.getSimpleName();


    private final String URL_RSS = "http://www.heise.de/newsticker/heise-top-atom.xml";


    private ListView liste;


    private static Content myContent;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rssmain);


        liste =(ListView)findViewById(R.id.feedListe);

        myContent = new Content();


        new ReadRssFeedTask(this).execute();


        liste.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,int position, long    id) {


                Intent intent = new Intent(RSSMain.this,Anzeigen.class);

                intent.putExtra("URL", myContent.getUrl(position));
                startActivity(intent);
            }
        });
    }
    private class ReadRssFeedTask extends AsyncTask<Void,Void,Void> {


        private ProgressDialog dialog;


        private  HttpClient client;


        private HttpGet httpGet;


        private HttpResponse response;

        //Konstruktor
        public ReadRssFeedTask(Activity activity) {

            client = new DefaultHttpClient();

            httpGet = new HttpGet(URL_RSS);

            response = null;

            dialog = new ProgressDialog(activity);
        }


        protected void onPreExecute() {


            dialog.setTitle("Bitte warten!");
            dialog.setMessage("Der RSS Feed wird vom Server geladen.");
            dialog.show();
        }


        @Override
        protected Void doInBackground(Void... tmp){

            try {


                response = client.execute(httpGet);

                } catch (ClientProtocolException e) {
                Log.e(tag,"Falsches Protokol " + e.getMessage());
                } catch (IOException e) {
                Log.e(tag,"URL ist falsch, URL: "+URL_RSS + " " + e.getMessage());
            }


            if(response != null){


                StatusLine statusLine = response.getStatusLine();


                if ( statusLine.getStatusCode() == 200) {


                    HttpEntity entity = response.getEntity();


                    try {

                        SAXParserFactory spf = SAXParserFactory.newInstance();

                        SAXParser sp = null;

                        try {

                            sp = spf.newSAXParser();

                            } catch (ParserConfigurationException e) {
                            Log.e(tag, "Fehler: " + e.getMessage());
                            } catch (SAXException e) {
                            Log.e(tag, "Fehler im Handler: " + e.getMessage());
                        }


                        Handler myHandler = new Handler();

                        try {

                            sp.parse(entity.getContent(), myHandler);

                            } catch (SAXException e) {
                            Log.e(tag,"Fehler beim Parsen. Fehler: " + e.getMessage());
                            } catch (IOException e) {
                            Log.e(tag,"URL: "+ URL_RSS + " konnte nicht geöffnet werde! "+ e.getMessage());
                        }


                        } catch (IllegalStateException e) {
                        e.printStackTrace();
                    }


                    }else{
                    Log.i(RSSMain.class.getSimpleName(),"Der Server antwortet mit anderen Statuscode als 200. Statuscode: "+statusLine.getStatusCode());
                }
                }else{
                Log.i(RSSMain.class.getSimpleName(),"Keine Internetverbindung.");

            }

            return null;

        }

        @Override
        protected void onPostExecute(Void result){


            if (dialog.isShowing()) {
                dialog.dismiss();
            }



            liste.setAdapter(new ArrayAdapter<String>(RSSMain.this, android.R.layout.simple_list_item_1,myContent.getTitel()));

        }
    }
}

为什么解析器不起作用?我找不到答案。

谢谢您的帮助

4

0 回答 0