How to make Rss News Reader application in android …? [closed]

送分小仙女□ 提交于 2019-12-08 06:35:41

问题


i have a sample code for parsing data from XML , In XML parsing SAX,DOC,and XML Pull parser which is the best XML parser ,what is json parser...? and how to parse news feeds through json parser.please answer with any examples

   public class MainActivity1 extends Activity {
   private String   

  finalUrl="http://downloads.bbc.co.uk/podcasts/worldservice/globalnews/rss.xml";
  private HandleXML obj;
  private TextView title,link,description;
  private LinearLayout mWeatherInfosLayout;

    @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   Intent intent = getIntent();
   finalUrl = intent.getStringExtra("url");
   setContentView(R.layout.activity_main_activity1);
   mWeatherInfosLayout = (LinearLayout) findViewById(R.id.weather_infos1);
   fetch();

   }

   @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 void fetch(){
   obj = new HandleXML(finalUrl);
  obj.fetchXML();
  mWeatherInfosLayout.removeAllViews();
  while(obj.parsingComplete)
  {     

      if(obj.gotit)
      {

        final LinearLayout forecastInfoLayout = (LinearLayout) 
                getLayoutInflater().inflate(R.layout.feedlist, null);

        final TextView tvWeather1 = (TextView)   
  forecastInfoLayout.findViewById(R.id.textview_forecast_info1);

        final TextView tvWeather2 = (TextView) 
  forecastInfoLayout.findViewById(R.id.textview_forecast_info2);

        final TextView tvWeather3 = (TextView) 
  forecastInfoLayout.findViewById(R.id.textview_forecast_info3);

        final TextView tvWeather4 = (TextView) 
    forecastInfoLayout.findViewById(R.id.textview_forecast_info4);

        tvWeather1.setText(obj.getTitle());
        tvWeather4.setText(obj.getPubDate());
        tvWeather2.setText(obj.getLink());
        String string = obj.getDescription();
        String[] parts = string.split("<");
        String part1 = parts[0];
        tvWeather3.setText(part1);
        mWeatherInfosLayout.addView(forecastInfoLayout);
        obj.setgotit();
        }
     }

     }
     }

回答1:


Change your HandleXML Class accordingly.

public class HandleXML {

   private String title = "title";
   private String link = "link";
   private String description = "description";

   private String urlString = null;
   private XmlPullParserFactory xmlFactoryObject;
   public volatile boolean parsingComplete = true;
   public HandleXML(String url){
      this.urlString = url;
   }
    public String getTitle(){
     return title;
   }
   public String getLink(){
    return link;
  }
  public String getDescription(){
    return description;
 }
   public void parseXMLAndStoreIt(XmlPullParser myParser) {
    int event;
    String text=null;
     try {
      event = myParser.getEventType();
       while (event != XmlPullParser.END_DOCUMENT) {
        String name=myParser.getName();
          switch (event){
          case XmlPullParser.START_TAG:
          break;
        case XmlPullParser.TEXT:
           text = myParser.getText();
        break;
        case XmlPullParser.END_TAG:
           if(name.equals("title")){
              title = text;
           }
           else if(name.equals("link")){    
              link = text;
           }
           else if(name.equals("description")){
              description = text;
           }
           else{
           }
           break;
     }       
     event = myParser.next(); 
   }
   parsingComplete = false;
  } catch (Exception e) {
     e.printStackTrace();
  }
  }
   public void fetchXML(){
     Thread thread = new Thread(new Runnable(){
 @Override
 public void run() {
  try {
     URL url = new URL(urlString);
     HttpURLConnection conn = (HttpURLConnection) url.openConnection();
     conn.setReadTimeout(10000 /* milliseconds */);
     conn.setConnectTimeout(15000 /* milliseconds */);
     conn.setRequestMethod("GET");
     conn.setDoInput(true);
     // Starts the query
     conn.connect();
     InputStream stream = conn.getInputStream();
     xmlFactoryObject = XmlPullParserFactory.newInstance();
     XmlPullParser myparser = xmlFactoryObject.newPullParser();
     myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
     myparser.setInput(stream, null);
     parseXMLAndStoreIt(myparser);
     stream.close();
    } catch (Exception e) {
    }
    }
    });
    thread.start(); 
 }
 }


来源:https://stackoverflow.com/questions/26397647/how-to-make-rss-news-reader-application-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!