@Override protected RssItem doInBackground(RssParser... params) { RssParser parser = params[0]; RssItem item = null; try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); xr.setContentHandler(parser); // chamar arquivo xml // xr.parse(new InputSource(activity.getAssets().open("image_of_the_day.xml"))); // chamar link na internet URL url = new URL("http://www.nasa.gov/rss/dyn/image_of_the_day.rss"); xr.parse(new InputSource(url.openConnection().getInputStream())); item = parser.getFirstItem(); if (item != null) { /*item.setImagem( BitmapFactory.decodeResource( activity.getResources(), R.drawable.image_of_the_day));*/ item.setImagem(getBitmap(item.getImagemUrl())); } } catch (Exception e) { e.printStackTrace(); } return item; }
/* (non-Javadoc) * @see javax.microedition.lcdui.ItemCommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Item) */ public void commandAction(Command command, Item item) { // System.out.println("DefaultRssItemCommandListner: command ="+ command.getLabel() ); if (command == RssTagHandler.CMD_RSS_ITEM_SELECT) { RssItem rssItem = (RssItem) UiAccess.getAttribute(item, RssItem.ATTRIBUTE_KEY); String rssUrl = rssItem.getLink(); if (rssUrl != null && this.rssBrowser != null) { this.rssBrowser.getRssTagHandler().onViewUrl(rssUrl, item); } if (rssItem != null && StyleSheet.display != null) { // #style rssDescriptionAlert Alert alert = new Alert( rssItem.getTitle(), rssItem.getDescription(), null, AlertType.INFO, de.enough.polish.ui.StyleSheet.rssdescriptionalertStyle); alert.setTimeout(Alert.FOREVER); alert.addCommand(RssTagHandler.CMD_GO_TO_ARTICLE); alert.addCommand(HtmlTagHandler.CMD_BACK); alert.setCommandListener(this); StyleSheet.display.setCurrent(alert); this.url = rssItem.getLink(); } } else { this.rssBrowser.handleCommand(command); } }
@Override protected void onListItemClick(ListView l, View v, int position, long id) { RssItem item = rssList.get(position); Intent intent = new Intent(RssReaderActivity.this, RssDetailActivity.class); intent.putExtra("rss_title", item.getTitle()); intent.putExtra("rss_link", item.getLink()); RssReaderActivity.this.startActivity(intent); }
/** * Called when user clicks an item in the list. Starts an activity to open the url for that item. */ @Override protected void onListItemClick(ListView l, View v, int position, long id) { // Wrokround since PullToRefreshView already has one item in this list. RssItem item = mAdapter.getItem(position - 1); // Creates and starts an intent to open the item.link url. Intent intent = new Intent(); intent.setClass(this, DetailActivity.class); intent.putExtra("data", item.getDescription()); startActivity(intent); }
@Override public void characters(char[] ch, int start, int length) throws SAXException { if (parsingTitle) { if (currentItem != null) currentItem.setTitle(new String(ch, start, length)); } else if (parsingLink) { if (currentItem != null) { currentItem.setLink(new String(ch, start, length)); parsingLink = false; } } }
@Override public void endElement(String uri, String localName, String qName) { if (localName.equalsIgnoreCase("image")) { if (!isItem) { isImage = false; } } else if (localName.equalsIgnoreCase("url")) { if (isImage) { feed.setLogo(StringEscapeUtils.unescapeXml(chars.toString())); } } else if (localName.equalsIgnoreCase("ttl")) { if (!isItem) { feed.setTtl(Integer.parseInt(StringEscapeUtils.unescapeXml(chars.toString()))); } } else if (localName.equalsIgnoreCase("title")) { if (!isImage) { if (isItem) { item.setTitle(StringEscapeUtils.unescapeXml(chars.toString())); } else { feed.setTitle(StringEscapeUtils.unescapeXml(chars.toString())); } } } else if (localName.equalsIgnoreCase("description")) { if (isItem) { item.setDescription(StringEscapeUtils.unescapeXml(chars.toString())); } else { feed.setDescription(StringEscapeUtils.unescapeXml(chars.toString())); } } else if (localName.equalsIgnoreCase("pubDate")) { if (isItem) { try { item.setDate(dateFormat.parse(StringEscapeUtils.unescapeXml(chars.toString()))); } catch (Exception e) { Log.e(LOG_CAT, "pubDate", e); } } else { try { feed.setDate(dateFormat.parse(StringEscapeUtils.unescapeXml(chars.toString()))); } catch (Exception e) { Log.e(LOG_CAT, "pubDate", e); } } } else if (localName.equalsIgnoreCase("item")) { isItem = false; } else if (localName.equalsIgnoreCase("link")) { if (isItem) { item.setLink(StringEscapeUtils.unescapeXml(chars.toString())); } else { feed.setLink(StringEscapeUtils.unescapeXml(chars.toString())); } } if (localName.equalsIgnoreCase("item")) {} }
//Goes through character by character when parsing whats inside of a tag. @Override public void characters(char[] ch, int start, int length) throws SAXException { if (currentItem != null) { //If parsingTitle is true, then that means we are inside a <title> tag so the text is the title of an item. if (parsingTitle) currentItem.setTitle(new String(ch, start, length)); //If parsingLink is true, then that means we are inside a <link> tag so the text is the link of an item. else if (parsingLink) currentItem.setLink(new String(ch, start, length)); //If parsingDescription is true, then that means we are inside a <description> tag so the text is the description of an item. else if (parsingDescription) currentItem.setDescription(new String(ch, start, length)); else if (parsingPubDate) currentItem.setPubDate(new String(ch, start, length)); } }
/** * This is called to render a particular item for the on screen list. Uses an off-the-shelf * TwoLineListItem view, which contains text1 and text2 TextViews. We pull data from the RssItem * and set it into the view. The convertView is the view from a previous getView(), so we can * re-use it. * * @see ArrayAdapter#getView */ @Override public View getView(int position, View convertView, ViewGroup parent) { TwoLineListItem view; // Here view may be passed in for re-use, or we make a new one. if (convertView == null) { view = (TwoLineListItem) mInflater.inflate(android.R.layout.simple_list_item_2, null); } else { view = (TwoLineListItem) convertView; } RssItem item = this.getItem(position); view.getText1().setText(item.getTitle()); return view; }
public void displayData(RssItem item) { TextView titulo = (TextView) findViewById(R.id.titulo); TextView data = (TextView) findViewById(R.id.data); TextView descricao = (TextView) findViewById(R.id.descricao); ImageView imagem = (ImageView) findViewById(R.id.imagem); if (item != null) { titulo.setText(item.getTitulo()); data.setText(item.getData()); descricao.setText(item.getDescricao()); imagem.setImageBitmap(item.getImagem()); } else { titulo.setText(null); data.setText(null); descricao.setText(null); imagem.setImageBitmap(null); } }
public boolean insertToDB(RssItem item) { // query if it already exit Cursor cursor = getContentResolver() .query( RSSApp.RssItems.CONTENT_URI, PROJECTION, RSSApp.RssItems.COLUMN_NAME_PUBDATE + "=" + "'" + item.getPubDate() + "'", null, null); int count = 0; if (cursor != null) { count = cursor.getCount(); } if (count == 0) { ContentValues values = new ContentValues(); values.put(RSSApp.RssItems.COLUMN_NAME_TITLE, item.getTitle()); values.put(RSSApp.RssItems.COLUMN_NAME_DESCRIPTION, item.getDescription()); values.put(RSSApp.RssItems.COLUMN_NAME_PUBDATE, item.getPubDate()); Uri newUri = getContentResolver().insert(RSSApp.RssItems.CONTENT_URI, values); if (newUri != null) { log("insertToDB: " + newUri); return true; } else { log("insertToDb: fail!"); } } else { mHandler.post( new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "no more update !", Toast.LENGTH_SHORT) .show(); } }); log("DB already has this record : title = " + item.getTitle()); } return false; }
//Called when an opening tag is reached, such as <item> or <title> @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equals("item")) currentItem = new RssItem(); else if (qName.equals("title")) parsingTitle = true; else if (qName.equals("link")) parsingLink = true; else if (qName.equals("description")) parsingDescription = true; else if (qName.equals("pubDate")) parsingPubDate = true; else if (qName.equals("media:thumbnail") || qName.equals("media:content") || qName.equals("image")) { if (attributes.getValue("url") != null) currentItem.setImageUrl(attributes.getValue("url")); } }
public static List<RssItem> parse(String rssFeed, Activity activity) { try { Thread.sleep(6000); } catch (InterruptedException e1) { e1.printStackTrace(); } List<RssItem> list = new ArrayList<RssItem>(); XmlPullParser parser = Xml.newPullParser(); InputStream stream = null; try { stream = new URL(rssFeed).openStream(); } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { // auto-detect the encoding from the stream parser.setInput(stream, null); int eventType = parser.getEventType(); boolean done = false; RssItem item = null; while (eventType != XmlPullParser.END_DOCUMENT && !done) { String name = null; switch (eventType) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: name = parser.getName(); if (name.equalsIgnoreCase(ITEM)) { Log.i("new item", "Create new item"); item = new RssItem(); } else if (item != null) { if (name.equalsIgnoreCase(LINK)) { Log.i("Attribute", "setLink"); item.setLink(parser.nextText()); } else if (name.equalsIgnoreCase(DESCRIPTION)) { Log.i("Attribute", "description"); item.setDescription(parser.nextText().trim()); } else if (name.equalsIgnoreCase(PUB_DATE)) { Log.i("Attribute", "date"); item.setPubDate(parser.nextText()); } else if (name.equalsIgnoreCase(TITLE)) { Log.i("Attribute", "title"); item.setTitle(parser.nextText().trim()); } } break; case XmlPullParser.END_TAG: name = parser.getName(); Log.i("End tag", name); if (name.equalsIgnoreCase(ITEM) && item != null) { Log.i("Added", item.toString()); list.add(item); } else if (name.equalsIgnoreCase(CHANNEL)) { done = true; } break; } eventType = parser.next(); } } catch (Exception e) { throw new RuntimeException(e); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } } return list; }
/** rss item 문자열 생성 */ private static String rssItemStr(RssItem item) { StringBuilder buffer = new StringBuilder(); buffer.append(" "); // 들여쓰기용 buffer.append("<item>"); if (item.getTitle() != null && !"".equals(item.getTitle())) buffer.append("<title>" + "<![CDATA[" + item.getTitle() + "]]>" + "</title>"); if (item.getLink() != null && !"".equals(item.getLink())) buffer.append("<link>" + item.getLink() + "</link>"); if (item.getDescription() != null && !"".equals(item.getDescription())) buffer.append( "<description>" + "<![CDATA[" + item.getDescription().replaceAll(BR, "") + "]]>" + "</description>"); if (item.getAuthor() != null && !"".equals(item.getAuthor())) buffer.append("<author>" + item.getAuthor() + "</author>"); if (item.getCategory() != null && !"".equals(item.getCategory())) buffer.append("<category>" + "<![CDATA[" + item.getCategory() + "]]>" + "</category>"); if (item.getLink() != null && !"".equals(item.getLink())) buffer.append("<guid>" + item.getLink() + "</guid>"); if (item.getPubDate() != null) buffer.append("<pubDate>" + toRfc822DateFormat(item.getPubDate()) + "</pubDate>"); buffer.append("</item>"); return buffer.toString(); }