2013-07-27 27 views
0

我有一个问题,我没有自己解决它!如何从URL获取xml文件作为输入流并进行xml解析

我想在我的应用程序上获得RSS源,所以我把这个Url放在我的代码中,以便得到xml文件,然后解析... Rss Feeds

我成功从Url获​​得xml数据,这是从后台方法中返回的字符串,但问题是当我做了解析,而非xmlParser方法中的情况实现!下面是我的代码和堆栈跟踪:

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! 

依此类推......直到文档结束,为什么在解析时出现错误?

回答

1

我认为switch语句工作正常,但是您的代码还有其他问题。首先,你错过了break声明在最后一个非默认情况下:

   case XmlPullParser.END_TAG: 
       tagName = parser.getName(); 
       if (tagName.equalsIgnoreCase("item") && currentFeed != null){ 
        feeds.add(currentFeed); 
       } 
       //<---- here! 

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

该日志语句正在运行每次都发现结束标记时间,这就是为什么你会得到所有的日志信息。

真正的问题是,你正在做字符串比较与==而不是使用.equals(),例如:

    if (tagName == "item") { //<-- this will never be true 
        currentFeed = new NewsFeeds(); 
       } 

开关那些是"item".equals(tagName)