0

問題があり、自分で解決できませんでした!.

アプリケーションで RSS フィードを取得したいので、この URL をコードに挿入して xml ファイルを取得し、解析を行います... Rss フィード

do in background メソッドから返された文字列である Url から xml データを取得することに成功しましたが、xmlParser メソッドが実装されている場合とは異なり、解析を行ったときに問題が発生しました。以下はすべて私のコードと StackTrace です。

MainActivity クラス:

package com.example.testfeeds;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

@SuppressWarnings("unused")
public class MainActivity extends Activity {

    String name;
    Button getfeeds;
    ArrayList<NewsFeeds> feeds;
    ListView feedsList;
    ArrayAdapter<NewsFeeds> adapter;
    ProgressDialog progress ;



    @SuppressWarnings("null")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getfeeds = (Button) findViewById(R.id.button1);
        feedsList=(ListView) findViewById(R.id.listView1);


        progress = new ProgressDialog(this); 
        progress.setMessage("Loading .... "); 
        progress.setCancelable(false); 


        getfeeds.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

            new GetFeeds().execute("http://yunn.yu.edu.jo/index.php?option=com_content&view=category&id=55&layout=blog&Itemid=104&format=feed&type=rss"); 

            }
        });

        feedsList.setOnItemClickListener(new OnItemClickListener() {

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

                NewsFeeds item = (NewsFeeds) adapter.getItemAtPosition(position);

                String url= item.getLink();

                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

                startActivity(browserIntent);

            }
        });



    }

////////////////////////////////////////////////////////////////////////////

     class GetFeeds extends AsyncTask<String, Integer, String>{


         @Override
            protected void onPreExecute() {

                progress.show();
                super.onPreExecute();
            }

        @Override
        protected String doInBackground(String... arg0) {

            String xmlData = GetUrlBody(arg0[0]); 

            Log.d("msg","in doInBackground .. ");


            return xmlData ;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {

            super.onProgressUpdate(values);
        }


        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            Log.d("msg","the result not null");

            ////--------------------------------------////

            //Log.d("msg",result);

            FileOutputStream makeXml = null ; //declare a file to store the incoming xml data.
            InputStream xmlFile = null; // declare an input stream to get the xml file.
            try {


                makeXml= openFileOutput("temp.xml", Context.MODE_PRIVATE);
                makeXml.write(result.getBytes());
                makeXml.flush();
                makeXml.close(); 

                xmlFile= openFileInput("temp.xml"); 

//              StringBuilder sb = new StringBuilder(); 
//              int a =0 ; 
//              while ((a=xmlFile.read())!=-1) {
//                   sb.append((char)a);
//                  
//              }
//              
//              
//              
//              Log.d("msg",sb.toString());


            } catch (IOException e) {


                e.printStackTrace();
            }

          ////--------------------------------------////


            XmlPullParserFactory pullParserFactory;

            try {
                pullParserFactory = XmlPullParserFactory.newInstance();
                XmlPullParser parser = pullParserFactory.newPullParser();
                    parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
                    parser.setInput(xmlFile, null);
                    parseXML(parser);
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            } catch (IOException e) {

                e.printStackTrace();
            }


            progress.dismiss();

        }




     }


////////////////////////////////////////////////////////////////////////////

        String GetUrlBody (String Url ){

            Log.d("msg","in get url ..");

        HttpClient cli = new DefaultHttpClient(); 



        HttpGet g = new HttpGet(Url); 



        try{
        HttpResponse res = cli.execute(g);



        if(res.getStatusLine().getStatusCode() == 200){

            String s =
        EntityUtils.toString(res.getEntity(), HTTP.UTF_8); 

            return s; 

        }else {
            return "Not Found"; 
        }

        }catch(Exception exx){}


        return null; 
    }


////////////////////////////////////////////////////////////////////////////

        private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
        {

            int eventType = parser.getEventType();
            NewsFeeds currentFeed = null; 
            // int i=0;

            while (eventType != XmlPullParser.END_DOCUMENT){

                String tagName = null;

                switch (eventType){

                //----------------------------------//
                case XmlPullParser.START_DOCUMENT: 
                    feeds = new ArrayList<NewsFeeds>();
                    adapter = new ArrayAdapter<NewsFeeds>(this,R.layout.list_view,R.id.listView1, feeds);
                    break;
               //----------------------------------//
                 case XmlPullParser.START_TAG:
                     tagName = parser.getName();
                     if (tagName == "item"){
                         currentFeed = new NewsFeeds();
                     } else if (currentFeed != null){
                         if (tagName == "title"){
                             currentFeed.title = parser.nextText();
                             Log.d("value",currentFeed.title);
                         } else if (tagName == "link"){
                            currentFeed.link = parser.nextText();
                            Log.d("value",currentFeed.link);
                         } else if (tagName == "pubDate"){
                             currentFeed.feedDate= parser.nextText();
                             Log.d("value",currentFeed.feedDate);
                         }
                     }
                     break;                     
               //----------------------------------//
                 case XmlPullParser.END_TAG:
                     tagName = parser.getName();
                     if (tagName.equalsIgnoreCase("item") && currentFeed != null){
                        feeds.add(currentFeed);
                     }

              //----------------------------------//

                  default : Log.d("error","non of the cases implemented!");

                     break;

                }// end-switch.

                ///Log.d("success",feeds.get(0).getTitle());
                eventType= parser.next();
                //i++;
            }// end-while.



        } //end xmlParser method.


////////////////////////////////////////////////////////////////////////////


} // end- MainActivity class.

ニュースフィード クラス:

package com.example.testfeeds;

public class NewsFeeds {

    String title;
    String link;
    String feedDate;

    public void setTitle(String title) {
        this.title = title;
    }


    public void setLink(String link) {
        this.link = link;
    }


    public void setFeedDate(String feedDate) {
        this.feedDate = feedDate;
    }


    public String getFeedDate() {
        return feedDate;
    }


    public String getTitle() {
        return title;
    }


    public String getLink() {
        return link;
    }


}

スタックトレース:

07-27 13:10:29.685: D/dalvikvm(9308): GC_EXTERNAL_ALLOC freed 783 objects / 56336 bytes in 424ms
07-27 13:13:40.009: D/msg(9308): in get url ..
07-27 13:14:05.304: D/dalvikvm(9308): GC_FOR_MALLOC freed 1803 objects / 94784 bytes in 351ms
07-27 13:14:05.475: D/msg(9308): in doInBackground .. 
07-27 13:14:05.564: D/msg(9308): the result not null
07-27 13:14:06.164: D/dalvikvm(9308): GC_FOR_MALLOC freed 460 objects / 920152 bytes in 241ms
07-27 13:14:06.864: D/error(9308): non of the cases implemented!
07-27 13:14:06.864: D/error(9308): non of the cases implemented!
07-27 13:14:06.864: D/error(9308): non of the cases implemented!
07-27 13:14:06.887: D/error(9308): non of the cases implemented!
07-27 13:14:06.894: D/error(9308): non of the cases implemented!
07-27 13:14:06.894: D/error(9308): non of the cases implemented!
07-27 13:14:06.894: D/error(9308): non of the cases implemented!
07-27 13:14:06.894: D/error(9308): non of the cases implemented!
07-27 13:14:06.894: D/error(9308): non of the cases implemented!
07-27 13:14:06.894: D/error(9308): non of the cases implemented!
07-27 13:14:06.915: D/error(9308): non of the cases implemented!
07-27 13:14:06.915: D/error(9308): non of the cases implemented!
07-27 13:14:06.935: D/error(9308): non of the cases implemented!
07-27 13:14:06.935: D/error(9308): non of the cases implemented!
07-27 13:14:06.944: D/error(9308): non of the cases implemented!
07-27 13:14:06.944: D/error(9308): non of the cases implemented!
07-27 13:14:06.984: D/error(9308): non of the cases implemented!
07-27 13:14:06.984: D/error(9308): non of the cases implemented!
07-27 13:14:06.994: D/error(9308): non of the cases implemented!
07-27 13:14:06.994: D/error(9308): non of the cases implemented!
07-27 13:14:07.007: D/error(9308): non of the cases implemented!
07-27 13:14:07.030: D/error(9308): non of the cases implemented!
07-27 13:14:07.034: D/error(9308): non of the cases implemented!
07-27 13:14:07.034: D/error(9308): non of the cases implemented!
07-27 13:14:07.055: D/error(9308): non of the cases implemented!
07-27 13:14:07.055: D/error(9308): non of the cases implemented!
07-27 13:14:07.055: D/error(9308): non of the cases implemented!
07-27 13:14:07.084: D/error(9308): non of the cases implemented!
07-27 13:14:07.084: D/error(9308): non of the cases implemented!
07-27 13:14:07.084: D/error(9308): non of the cases implemented!
07-27 13:14:07.104: D/error(9308): non of the cases implemented!
07-27 13:14:07.125: D/error(9308): non of the cases implemented!
07-27 13:14:07.125: D/error(9308): non of the cases implemented!
07-27 13:14:07.125: D/error(9308): non of the cases implemented!
07-27 13:14:07.148: D/error(9308): non of the cases implemented!
07-27 13:14:07.148: D/error(9308): non of the cases implemented!
07-27 13:14:07.174: D/error(9308): non of the cases implemented!
07-27 13:14:07.174: D/error(9308): non of the cases implemented!
07-27 13:14:07.174: D/error(9308): non of the cases implemented!
07-27 13:14:07.174: D/error(9308): non of the cases implemented!
07-27 13:14:07.174: D/error(9308): non of the cases implemented!
07-27 13:14:07.174: D/error(9308): non of the cases implemented!
07-27 13:14:07.174: D/error(9308): non of the cases implemented!
07-27 13:14:07.174: D/error(9308): non of the cases implemented!
07-27 13:14:07.174: D/error(9308): non of the cases implemented!
07-27 13:14:07.197: D/error(9308): non of the cases implemented!
07-27 13:14:07.197: D/error(9308): non of the cases implemented!
07-27 13:14:07.197: D/error(9308): non of the cases implemented!
07-27 13:14:07.225: D/error(9308): non of the cases implemented!
07-27 13:14:07.225: D/error(9308): non of the cases implemented!
07-27 13:14:07.225: D/error(9308): non of the cases implemented!

などなど..ドキュメントの最後まで、解析中にエラーが発生するのはなぜですか?

4

1 に答える 1