如何使RSS新闻阅读器应用程序在Android中....?

编程入门 行业动态 更新时间:2024-10-26 02:34:55
本文介绍了如何使RSS新闻阅读器应用程序在Android中....?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个样品code从XML解析数据,在XML解析SAX,DOC和XML解析器拉这是最好的XML解析器,什么是JSON解析器...?以及如何解析的消息通过与任何例子JSON parser.please答案供稿

公共类MainActivity1延伸活动{   私人字符串  finalUrl =htt​​p://downloads.bbc.co.uk/podcasts/worldservice/globalnews/rss.xml;  私人HandleXML OBJ;  私人TextView的标题,链接,描述;  私人的LinearLayout mWeatherInfosLayout;    @覆盖   保护无效的onCreate(捆绑savedInstanceState){   super.onCreate(savedInstanceState);   意向意图= getIntent();   finalUrl = intent.getStringExtra(URL);   的setContentView(R.layout.activity_main_activity1);   mWeatherInfosLayout =(的LinearLayout)findViewById(R.id.weather_infos1);   取();   }   @覆盖   公共布尔onCreateOptionsMenu(菜单菜单){  //充气菜单;如果是present这增加了项目操作栏。  。getMenuInflater()膨胀(R.menu.main,菜单);  返回true;}  公共无效取(){   OBJ =新HandleXML(finalUrl);  obj.fetchXML();  mWeatherInfosLayout.removeAllViews();  而(obj.parsingComplete)  {      如果(obj.gotit)      {        最终的LinearLayout forecastInfoLayout =(LinearLayout中)                getLayoutInflater()膨胀(R.layout.feedlist,NULL);        最终的TextView tvWeather1 =(TextView中)  forecastInfoLayout.findViewById(R.id.textview_forecast_info1);        最终的TextView tvWeather2 =(TextView中)  forecastInfoLayout.findViewById(R.id.textview_forecast_info2);        最终的TextView tvWeather3 =(TextView中)  forecastInfoLayout.findViewById(R.id.textview_forecast_info3);        最终的TextView tvWeather4 =(TextView中)    forecastInfoLayout.findViewById(R.id.textview_forecast_info4);        tvWeather1.setText(obj.getTitle());        tvWeather4.setText(obj.getPubDate());        tvWeather2.setText(obj.getLink());        串串= obj.getDescription();        的String [] =零件string.split(<);        第一部分的String =零件[0];        tvWeather3.setText(第一部分);        mWeatherInfosLayout.addView(forecastInfoLayout);        obj.setgotit();        }     }     }     }

解决方案

相应更改HandleXML类。

公共类HandleXML {   私人字符串标题=称号;   私人字符串链接=联系;   私人字符串描述=说明;   私人字符串urlString = NULL;   私人XmlPullParserFactory xmlFactoryObject;   公共挥发性布尔parsingComplete = TRUE;   公共HandleXML(字符串URL){      this.urlString =网址;   }    公共字符串的getTitle(){     返回称号;   }   公共字符串getLink(){    返回的链接;  }  公共字符串getDescription(){    返回描述; }   公共无效parseXMLAndStoreIt(XmlPullParser myParser){    INT事件;    字符串文本= NULL;     尝试{      事件= myParser.getEventType();       而(事件!= XmlPullParser.END_DOCUMENT){        字符串名称= myParser.getName();          开关(事件){          案例XmlPullParser.START_TAG:          打破;        案例XmlPullParser.TEXT:           文字= myParser.getText();        打破;        案例XmlPullParser.END_TAG:           如果(name.equals(标题)){              标题=文字;           }           否则,如果(name.equals(链接)){              链接=文本;           }           否则,如果(name.equals(说明)){              说明=文本;           }           其他{           }           打破;     }     事件= myParser.next();   }   parsingComplete = FALSE;  }赶上(例外五){     e.printStackTrace();  }  }   公共无效fetchXML(){     线程线程=新主题(新的Runnable(){ @覆盖 公共无效的run(){  尝试{     网址URL =新的URL(urlString);     HttpURLConnection的康恩=(HttpURLConnection类)url.openConnection();     conn.setReadTimeout(10000 / *毫秒* /);     conn.setConnectTimeout(15000 / *毫秒* /);     conn.setRequestMethod(GET);     conn.setDoInput(真);     //开始查询     conn.connect();     InputStream的流= conn.getInputStream();     xmlFactoryObject = XmlPullParserFactory.newInstance();     XmlPullParser myparser = xmlFactoryObject.newPullParser();     myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,FALSE);     myparser.setInput(流,NULL);     parseXMLAndStoreIt(myparser);     stream.close();    }赶上(例外五){    }    }    });    thread.start(); } }

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="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(); } } } }

解决方案

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(); } }

更多推荐

如何使RSS新闻阅读器应用程序在Android中....?

本文发布于:2023-11-11 21:12:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579577.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阅读器   应用程序   新闻   RSS   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!