Android 如何制作Rss提要解析器

Android 如何制作Rss提要解析器,android,Android,我正在开发一个解析Rss提要并将其转换为Json文件的应用程序,该文件显示在回收器视图中 如何直接显示Rss提要而不将其转换为Json 我已经创建了包含项和函数的外部类。这些类由用于填充数组映射的构造函数组成 这是我的主要活动: public class MainActivity extends AppCompatActivity { Toolbar toolbar; RecyclerView recyclerView; RSSObject rssObject; //RSS link pri

我正在开发一个解析Rss提要并将其转换为Json文件的应用程序,该文件显示在回收器视图中

如何直接显示Rss提要而不将其转换为Json

我已经创建了包含项和函数的外部类。这些类由用于填充数组映射的构造函数组成

这是我的主要活动:

public class MainActivity extends AppCompatActivity {

Toolbar toolbar;
RecyclerView recyclerView;
RSSObject rssObject;

//RSS link
private final String RSS_link="http://rss.nytimes.com/services/xml/rss/nyt/Science.xml";
private final String RSS_to_Json_API = "https://api.rss2json.com/v1/api.json?rss_url=";

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

    toolbar = (Toolbar)findViewById(R.id.toolbar);
    toolbar.setTitle("News");
    setSupportActionBar(toolbar);

    recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
    LinearLayoutManager linearLayoutManager  = new LinearLayoutManager(getBaseContext(),LinearLayoutManager.VERTICAL,false);
    recyclerView.setLayoutManager(linearLayoutManager);

    loadRSS();
}

private void loadRSS() {
    AsyncTask<String,String,String> loadRSSAsync = new AsyncTask<String, String, String>() {

        ProgressDialog mDialog = new ProgressDialog(MainActivity.this);

        @Override
        protected void onPreExecute() {
            mDialog.setMessage("Please wait...");
            mDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            String result;
            HTTPDataHandler http = new HTTPDataHandler();
            result = http.GetHTTPData(params[0]);
            return  result;
        }

        @Override
        protected void onPostExecute(String s) {
            mDialog.dismiss();
            rssObject = new Gson().fromJson(s,RSSObject.class);
            FeedAdapter adapter = new FeedAdapter(rssObject,getBaseContext());
            recyclerView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }
    };

    StringBuilder url_get_data = new StringBuilder(RSS_to_Json_API);
    url_get_data.append(RSS_link);
    loadRSSAsync.execute(url_get_data.toString());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu,menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId() == R.id.menu_refresh)
        loadRSS();
    return true;
}}
public类MainActivity扩展了AppCompatActivity{
工具栏;
回收视图回收视图;
RSSObject RSSObject;
//RSS链接
私有最终字符串RSS_link=”http://rss.nytimes.com/services/xml/rss/nyt/Science.xml";
私有最终字符串RSS_to_Json_API=”https://api.rss2json.com/v1/api.json?rss_url=";
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(toolbar)findviewbyd(R.id.toolbar);
工具栏。设置标题(“新闻”);
设置支持操作栏(工具栏);
recyclerView=(recyclerView)findViewById(R.id.recyclerView);
LinearLayoutManager LinearLayoutManager=新的LinearLayoutManager(getBaseContext(),LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(linearLayoutManager);
loadRSS();
}
私有void loadRSS(){
AsyncTask loadRSSAsync=new AsyncTask(){
ProgressDialog mDialog=新建ProgressDialog(MainActivity.this);
@凌驾
受保护的void onPreExecute(){
设置消息(“请稍候…”);
mDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串结果;
HTTPDataHandler http=新的HTTPDataHandler();
结果=http.GetHTTPData(参数[0]);
返回结果;
}
@凌驾
受保护的void onPostExecute(字符串s){
mDialog.discouse();
rssObject=new Gson().fromJson(s,rssObject.class);
FeedAdapter=新的FeedAdapter(rssObject,getBaseContext());
recyclerView.setAdapter(适配器);
adapter.notifyDataSetChanged();
}
};
StringBuilder url\u get\u data=新的StringBuilder(RSS\u to\u Json\u API);
url\u get\u data.append(RSS\u链接);
loadRSSAsync.execute(url_get_data.toString());
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(右菜单.主菜单,菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
if(item.getItemId()==R.id.menu\u刷新)
loadRSS();
返回true;
}}

Rss
xml
文件,因此如果您想从
Rss
提要中检索数据而不通过
json
,那么您需要从该
xml
文件中读取数据,以便使用此方法执行任务:

    public void parseXml(String xml){

    ArrayList<Articles> articles = new ArrayList();
    Articles currentArticle = new Articles();

    try {

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(false);
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput( new StringReader(xml)); // pass input whatever xml you have

        boolean insideItem = false;

        for(int eventType = xpp.getEventType(); eventType != 1; eventType = xpp.next()) {
            if(eventType == 2) {
                if(xpp.getName().equalsIgnoreCase("item")) {
                    insideItem = true;
                } else {
                    String pubDate;
                    if(xpp.getName().equalsIgnoreCase("title")) {
                        if(insideItem) {
                            pubDate = xpp.nextText();
                            currentArticle.setTitle(pubDate);
                        }
                    } else if(xpp.getName().equalsIgnoreCase("link")) {
                        if(insideItem) {
                            pubDate = xpp.nextText();
                            currentArticle.setLink(pubDate);
                        }
                    } else if(xpp.getName().equalsIgnoreCase("dc:creator")) {
                        if(insideItem) {
                            pubDate = xpp.nextText();
                            currentArticle.setAuthor(pubDate);
                        }
                    } else if(xpp.getName().equalsIgnoreCase("category")) {
                        if(insideItem) {
                            pubDate = xpp.nextText();
                            currentArticle.addCategory(pubDate);
                        }
                    } else if(xpp.getName().equalsIgnoreCase("media:thumbnail")) {
                        if(insideItem) {
                            pubDate = xpp.getAttributeValue((String)null, "url");
                            currentArticle.setImage(pubDate);
                        }
                    } else if(xpp.getName().equalsIgnoreCase("description")) {
                        if(insideItem) {
                            pubDate = xpp.nextText();
                            if(currentArticle.getImage() == null) {
                                currentArticle.setImage(getImageUrl(pubDate));
                            }

                            currentArticle.setDescription(pubDate);
                        }
                    } else if(xpp.getName().equalsIgnoreCase("content:encoded")) {
                        if(insideItem) {
                            pubDate = xpp.nextText();
                            if(currentArticle.getImage() == null) {
                                currentArticle.setImage(getImageUrl(pubDate));
                            }

                            currentArticle.setContent(pubDate);
                        }
                    } else if(xpp.getName().equalsIgnoreCase("pubDate")) {
                        Date pubDate1 = new Date(xpp.nextText());
                        currentArticle.setPubDate(pubDate1);
                    }
                }
            } else if(eventType == 3 && xpp.getName().equalsIgnoreCase("item")) {
                insideItem = false;
                articles.add(currentArticle);
                currentArticle = new Articles();
            }
        }
      itemList.setAdapter(new ListArticleAdapter(articles, MainActivity.this));
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
publicvoidparsexml(字符串xml){
ArrayList articles=新的ArrayList();
Articles currentArticle=新文章();
试一试{
XmlPullParserFactory工厂=XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp=factory.newPullParser();
setInput(新的StringReader(xml));//传递所有xml的输入
布尔值insideItem=false;
对于(int-eventType=xpp.getEventType();eventType!=1;eventType=xpp.next()){
if(eventType==2){
if(xpp.getName().equalsIgnoreCase(“项”)){
insideItem=真;
}否则{
字符串pubDate;
if(xpp.getName().equalsIgnoreCase(“标题”)){
如果(内部项目){
pubDate=xpp.nextText();
currentArticle.setTitle(发布日期);
}
}else if(xpp.getName().equalsIgnoreCase(“链接”)){
如果(内部项目){
pubDate=xpp.nextText();
currentArticle.setLink(pubDate);
}
}else if(xpp.getName().equalsIgnoreCase(“dc:creator”)){
如果(内部项目){
pubDate=xpp.nextText();
currentArticle.setAuthor(pubDate);
}
}else if(xpp.getName().equalsIgnoreCase(“类别”)){
如果(内部项目){
pubDate=xpp.nextText();
currentArticle.addCategory(pubDate);
}
}else if(xpp.getName().equalsIgnoreCase(“媒体:缩略图”)){
如果(内部项目){
pubDate=xpp.getAttributeValue((字符串)null,“url”);
currentArticle.setImage(pubDate);
}
}else if(xpp.getName().equalsIgnoreCase(“说明”)){
如果(内部项目){
pubDate=xpp.nextText();
if(currentArticle.getImage()==null){
setImage(getImageUrl(publidate));
}
currentArticle.setDescription(pubDate);
}
}else if(xpp.getName().equalsIgnoreCase(“内容:编码”)){
如果(内部项目){
pubDate=xpp.nextText();
if(currentArticle.getImage()==null){
setImage(getImageUrl(publidate));
}
currentArticle.setContent(pubDate);
}
}else if(xpp.getName().equalsIgnoreCase(“pubDate”)){
Date pubDate1=新日期(xpp.nextText());
currentArticle.setPubDate(pubDate1);
}
}
}否则,如果(夏娃)