Reputation: 266
I'm developing an app that parse an Rss feed and convert it into Json file that is showed in a recycler view.
How can I show directly the Rss feed without convert it in a Json?
I've created external classes that contain items and functions. These classes consist of constructors used to populate the array map.
This is my MainActivity:
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;
}}
Upvotes: 0
Views: 135
Reputation: 6073
Rss
is xml
file , so if you want to retreive data from Rss
feed without going through json
so you need to read from that xml
file , to do the job im using this method :
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();
}
}
So instead of using a list of RssObject
, im using a list of Articles
, hope you understand my code !
Upvotes: 1