Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.13 KB | None | 0 0
  1. import java.io.InputStream;
  2. import java.net.HttpURLConnection;
  3. import java.net.URL;
  4. import java.util.ArrayList;
  5.  
  6. import javax.xml.parsers.DocumentBuilder;
  7. import javax.xml.parsers.DocumentBuilderFactory;
  8.  
  9. import org.w3c.dom.DOMException;
  10. import org.w3c.dom.Document;
  11. import org.w3c.dom.Element;
  12. import org.w3c.dom.Node;
  13. import org.w3c.dom.NodeList;
  14.  
  15. import android.app.Activity;
  16. import android.app.ProgressDialog;
  17. import android.content.res.Resources;
  18. import android.graphics.drawable.Drawable;
  19. import android.os.Bundle;
  20. import android.os.Handler;
  21. import android.util.Log;
  22. import android.view.Menu;
  23. import android.view.View;
  24. import android.view.View.OnClickListener;
  25. import android.view.ViewGroup;
  26. import android.widget.AdapterView;
  27. import android.widget.Button;
  28. import android.widget.EditText;
  29. import android.widget.LinearLayout;
  30. import android.widget.Toast;
  31.  
  32. public class MainActivity extends Activity {
  33.  
  34. private static final String TAG = "MainActivity";
  35.  
  36. private static String rss_url = "http://www.thehindu.com/news/cities/chennai/chen-health/?service=rss";
  37.  
  38. ProgressDialog progressDialog;
  39. Handler handler = new Handler();
  40.  
  41. RSSListView list;
  42. RSSListAdapter adapter;
  43. ArrayList<RSSNewsItem> newsItemList;
  44.  
  45. @Override
  46. public void onCreate(Bundle savedInstanceState) {
  47. super.onCreate(savedInstanceState);
  48. setContentView(R.layout.activity_main);
  49.  
  50. ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
  51. ViewGroup.LayoutParams.MATCH_PARENT,
  52. ViewGroup.LayoutParams.MATCH_PARENT);
  53. list = new RSSListView(this);
  54.  
  55. adapter = new RSSListAdapter(this);
  56. list.setAdapter(adapter);
  57. list.setOnDataSelectionListener(new OnDataSelectionListener() {
  58. public void onDataSelected(AdapterView parent, View v,
  59. int position, long id) {
  60. RSSNewsItem curItem = (RSSNewsItem) adapter.getItem(position);
  61. String curTitle = curItem.getTitle();
  62.  
  63. Toast.makeText(getApplicationContext(),
  64. "Selected : " + curTitle, 1000).show();
  65. }
  66. });
  67.  
  68. newsItemList = new ArrayList<RSSNewsItem>();
  69. LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
  70. mainLayout.addView(list, params);
  71.  
  72. final EditText edit01 = (EditText) findViewById(R.id.edit01);
  73. edit01.setText(rss_url);
  74.  
  75. Button show_btn = (Button) findViewById(R.id.show_btn);
  76. show_btn.setOnClickListener(new OnClickListener() {
  77.  
  78. public void onClick(View v) {
  79. String inputStr = edit01.getText().toString();
  80. showRSS(inputStr);
  81. }
  82.  
  83. });
  84.  
  85. }
  86.  
  87. private void showRSS(String urlStr) {
  88. try {
  89. progressDialog = ProgressDialog.show(this, "RSS Refresh",
  90. "RSS Lodeing..", true, true);
  91.  
  92. RefreshThread thread = new RefreshThread(urlStr);
  93. thread.start();
  94.  
  95. } catch (Exception e) {
  96. Log.e(TAG, "Error", e);
  97. }
  98. }
  99.  
  100. class RefreshThread extends Thread {
  101. String urlStr;
  102.  
  103. public RefreshThread(String str) {
  104. urlStr = str;
  105. }
  106.  
  107. public void run() {
  108.  
  109. try {
  110. DocumentBuilderFactory builderFactory = DocumentBuilderFactory
  111. .newInstance();
  112. DocumentBuilder builder = builderFactory.newDocumentBuilder();
  113.  
  114. URL urlForHttp = new URL(urlStr);
  115.  
  116. InputStream instream = getInputStreamUsingHTTP(urlForHttp);
  117. // parse
  118. Document document = builder.parse(instream);
  119. int countItem = processDocument(document);
  120. Log.d(TAG, countItem + " news item processed.");
  121.  
  122. // post for the display of fetched RSS info.
  123. handler.post(updateRSSRunnable);
  124.  
  125. } catch (Exception ex) {
  126. ex.printStackTrace();
  127. }
  128.  
  129. }
  130. }
  131.  
  132. public InputStream getInputStreamUsingHTTP(URL url) throws Exception {
  133. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  134. conn.setRequestMethod("GET");
  135. conn.setDoInput(true);
  136. conn.setDoOutput(true);
  137. conn.setUseCaches(false);
  138. conn.setAllowUserInteraction(false);
  139.  
  140. int resCode = conn.getResponseCode();
  141. Log.d(TAG, "Response Code : " + resCode);
  142.  
  143. InputStream instream = conn.getInputStream();
  144.  
  145. return instream;
  146. }
  147.  
  148. private int processDocument(Document doc) {
  149. newsItemList.clear();
  150.  
  151. Element docEle = doc.getDocumentElement();
  152. NodeList nodelist = docEle.getElementsByTagName("item");
  153. int count = 0;
  154. if ((nodelist != null) && (nodelist.getLength() > 0)) {
  155. for (int i = 0; i < nodelist.getLength(); i++) {
  156.  
  157. RSSNewsItem newsItem = dissectNode(nodelist, i);
  158. if (newsItem != null) {
  159. newsItemList.add(newsItem);
  160. count++;
  161. }
  162. }
  163. }
  164.  
  165. return count;
  166. }
  167.  
  168. private RSSNewsItem dissectNode(NodeList nodelist, int index) {
  169. RSSNewsItem newsItem = null;
  170.  
  171. try {
  172. Element entry = (Element) nodelist.item(index);
  173.  
  174. Element title = (Element) entry.getElementsByTagName("title").item(
  175. 0);
  176. Element link = (Element) entry.getElementsByTagName("link").item(0);
  177. Element description = (Element) entry.getElementsByTagName(
  178. "description").item(0);
  179.  
  180. NodeList pubDataNode = entry.getElementsByTagName("pubDate");
  181. if (pubDataNode == null) {
  182. pubDataNode = entry.getElementsByTagName("dc:date");
  183. }
  184. Element pubDate = (Element) pubDataNode.item(0);
  185.  
  186. Element author = (Element) entry.getElementsByTagName("author")
  187. .item(0);
  188. Element category = (Element) entry.getElementsByTagName("category")
  189. .item(0);
  190.  
  191. String titleValue = null;
  192. if (title != null) {
  193. Node firstChild = title.getFirstChild();
  194. if (firstChild != null) {
  195. titleValue = firstChild.getNodeValue();
  196. }
  197. }
  198. String linkValue = null;
  199. if (link != null) {
  200. Node firstChild = link.getFirstChild();
  201. if (firstChild != null) {
  202. linkValue = firstChild.getNodeValue();
  203. }
  204. }
  205.  
  206. String descriptionValue = null;
  207. if (description != null) {
  208. Node firstChild = description.getFirstChild();
  209. if (firstChild != null) {
  210. descriptionValue = firstChild.getNodeValue();
  211. }
  212. }
  213.  
  214. String pubDateValue = null;
  215. if (pubDate != null) {
  216. Node firstChild = pubDate.getFirstChild();
  217. if (firstChild != null) {
  218. pubDateValue = firstChild.getNodeValue();
  219. }
  220. }
  221.  
  222. String authorValue = null;
  223. if (author != null) {
  224. Node firstChild = author.getFirstChild();
  225. if (firstChild != null) {
  226. authorValue = firstChild.getNodeValue();
  227. }
  228. }
  229.  
  230. String categoryValue = null;
  231. if (category != null) {
  232. Node firstChild = category.getFirstChild();
  233. if (firstChild != null) {
  234. categoryValue = firstChild.getNodeValue();
  235. }
  236. }
  237.  
  238. Log.d(TAG, "item node : " + titleValue + ", " + linkValue + ", "
  239. + descriptionValue + ", " + pubDateValue + ", "
  240. + authorValue + ", " + categoryValue);
  241.  
  242. newsItem = new RSSNewsItem(titleValue, linkValue, descriptionValue,
  243. pubDateValue, authorValue, categoryValue);
  244.  
  245. } catch (DOMException e) {
  246. e.printStackTrace();
  247. }
  248.  
  249. return newsItem;
  250. }
  251.  
  252. Runnable updateRSSRunnable = new Runnable() {
  253. public void run() {
  254.  
  255. try {
  256.  
  257. Resources res = getResources();
  258. Drawable rssIcon = res.getDrawable(R.drawable.rss_icon);
  259. for (int i = 0; i < newsItemList.size(); i++) {
  260. RSSNewsItem newsItem = (RSSNewsItem) newsItemList.get(i);
  261. newsItem.setIcon(rssIcon);
  262. adapter.addItem(newsItem);
  263. }
  264.  
  265. adapter.notifyDataSetChanged();
  266.  
  267. progressDialog.dismiss();
  268. } catch (Exception ex) {
  269. ex.printStackTrace();
  270. }
  271.  
  272. }
  273. };
  274.  
  275. @Override
  276. public boolean onCreateOptionsMenu(Menu menu) {
  277. // Inflate the menu; this adds items to the action bar if it is present.
  278. getMenuInflater().inflate(R.menu.main, menu);
  279. return true;
  280. }
  281.  
  282. }
  283.  
  284. public interface OnDataSelectionListener {
  285.  
  286.  
  287. public void onDataSelected(AdapterView parent, View v, int position, long id);
  288.  
  289. }
  290.  
  291. public class RSSListAdapter extends BaseAdapter {
  292.  
  293. private Context mContext;
  294.  
  295. private List<RSSNewsItem> mItems = new ArrayList<RSSNewsItem>();
  296.  
  297. public RSSListAdapter(Context context) {
  298. mContext = context;
  299. }
  300.  
  301. public void addItem(RSSNewsItem it) {
  302. mItems.add(it);
  303. }
  304.  
  305. public void setListItems(List<RSSNewsItem> lit) {
  306. mItems = lit;
  307. }
  308.  
  309. public int getCount() {
  310. return mItems.size();
  311. }
  312.  
  313. public Object getItem(int position) {
  314. return mItems.get(position);
  315. }
  316.  
  317. public boolean areAllItemsSelectable() {
  318. return false;
  319. }
  320.  
  321. public boolean isSelectable(int position) {
  322. return true;
  323. }
  324.  
  325. public long getItemId(int position) {
  326. return position;
  327. }
  328.  
  329. public View getView(int position, View convertView, ViewGroup parent) {
  330. RSSNewsItemView itemView;
  331. if (convertView == null) {
  332. itemView = new RSSNewsItemView(mContext, mItems.get(position));
  333. } else {
  334. itemView = (RSSNewsItemView) convertView;
  335.  
  336. itemView.setIcon(mItems.get(position).getIcon());
  337. itemView.setText(0, mItems.get(position).getTitle());
  338. itemView.setText(1, mItems.get(position).getPubDate());
  339. itemView.setText(2, mItems.get(position).getCategory());
  340. itemView.setText(3, mItems.get(position).getDescription());
  341. }
  342.  
  343. return itemView;
  344. }
  345.  
  346. }
  347.  
  348. public class RSSListView extends ListView {
  349. /**
  350. * DataAdapter for this instance
  351. */
  352. private RSSListAdapter adapter;
  353.  
  354. /**
  355. * Listener for data selection
  356. */
  357. private OnDataSelectionListener selectionListener;
  358.  
  359. public RSSListView(Context context) {
  360. super(context);
  361.  
  362. init();
  363. }
  364.  
  365. public RSSListView(Context context, AttributeSet attrs) {
  366. super(context, attrs);
  367.  
  368. init();
  369. }
  370.  
  371. /**
  372. * set initial properties
  373. */
  374. private void init() {
  375. // set OnItemClickListener for processing OnDataSelectionListener
  376. setOnItemClickListener(new OnItemClickAdapter());
  377. }
  378.  
  379. /**
  380. * set DataAdapter
  381. *
  382. * @param adapter
  383. */
  384. public void setAdapter(BaseAdapter adapter) {
  385. super.setAdapter(adapter);
  386.  
  387. }
  388.  
  389. /**
  390. * get DataAdapter
  391. *
  392. * @return
  393. */
  394. public BaseAdapter getAdapter() {
  395. return (BaseAdapter) super.getAdapter();
  396. }
  397.  
  398. /**
  399. * set OnDataSelectionListener
  400. *
  401. * @param listener
  402. */
  403. public void setOnDataSelectionListener(OnDataSelectionListener listener) {
  404. this.selectionListener = listener;
  405. }
  406.  
  407. /**
  408. * get OnDataSelectionListener
  409. *
  410. * @return
  411. */
  412. public OnDataSelectionListener getOnDataSelectionListener() {
  413. return selectionListener;
  414. }
  415.  
  416. class OnItemClickAdapter implements OnItemClickListener {
  417.  
  418. public OnItemClickAdapter() {
  419.  
  420. }
  421.  
  422. public void onItemClick(AdapterView parent, View v, int position,
  423. long id) {
  424.  
  425. if (selectionListener == null) {
  426. return;
  427. }
  428.  
  429. // get row and column
  430. int rowIndex = -1;
  431. int columnIndex = -1;
  432.  
  433. // call the OnDataSelectionListener method
  434. selectionListener.onDataSelected(parent, v, position, id);
  435.  
  436. }
  437.  
  438. }
  439.  
  440. }
  441.  
  442. public class RSSNewsItem {
  443.  
  444. private String title;
  445. private String link;
  446. private String description;
  447. private String pubDate;
  448. private String author;
  449. private String category;
  450.  
  451. private Drawable mIcon;
  452.  
  453. /**
  454. * Initialize with icon and data array
  455. */
  456. public RSSNewsItem() {
  457. }
  458.  
  459. /**
  460. * Initialize with icon and strings
  461. */
  462. public RSSNewsItem(String title, String link, String description, String pubDate, String author, String category) {
  463. this.title = title;
  464. this.link = link;
  465. this.description = description;
  466. this.pubDate = pubDate;
  467. this.author = author;
  468. this.category = category;
  469. }
  470.  
  471. /**
  472. * Set icon
  473. *
  474. * @param icon
  475. */
  476. public void setIcon(Drawable icon) {
  477. mIcon = icon;
  478. }
  479.  
  480. /**
  481. * Get icon
  482. *
  483. * @return
  484. */
  485. public Drawable getIcon() {
  486. return mIcon;
  487. }
  488.  
  489. public String getTitle() {
  490. return title;
  491. }
  492.  
  493. public void setTitle(String title) {
  494. this.title = title;
  495. }
  496.  
  497. public String getLink() {
  498. return link;
  499. }
  500.  
  501. public void setLink(String link) {
  502. this.link = link;
  503. }
  504.  
  505. public String getDescription() {
  506. return description;
  507. }
  508.  
  509. public void setDescription(String description) {
  510. this.description = description;
  511. }
  512.  
  513. public String getPubDate() {
  514. return pubDate;
  515. }
  516.  
  517. public void setPubDate(String pubDate) {
  518. this.pubDate = pubDate;
  519. }
  520.  
  521. public String getAuthor() {
  522. return author;
  523. }
  524.  
  525. public void setAuthor(String author) {
  526. this.author = author;
  527. }
  528.  
  529. public String getCategory() {
  530. return category;
  531. }
  532.  
  533. public void setCategory(String category) {
  534. this.category = category;
  535. }
  536.  
  537. /**
  538. * Compare with the input object
  539. *
  540. * @param other
  541. * @return
  542. */
  543. public int compareTo(RSSNewsItem other) {
  544. if (title.equals(other.getTitle())) {
  545. return -1;
  546. } else if (link.equals(other.getLink())) {
  547. return -1;
  548. } else if (description.equals(other.getDescription())) {
  549. return -1;
  550. } else if (pubDate.equals(other.getPubDate())) {
  551. return -1;
  552. } else if (author.equals(other.getAuthor())) {
  553. return -1;
  554. } else if (category.equals(other.getCategory())) {
  555. return -1;
  556. }
  557.  
  558. return 0;
  559. }
  560.  
  561. }
  562.  
  563. public class RSSNewsItem {
  564.  
  565. private String title;
  566. private String link;
  567. private String description;
  568. private String pubDate;
  569. private String author;
  570. private String category;
  571.  
  572. private Drawable mIcon;
  573.  
  574. /**
  575. * Initialize with icon and data array
  576. */
  577. public RSSNewsItem() {
  578. }
  579.  
  580. /**
  581. * Initialize with icon and strings
  582. */
  583. public RSSNewsItem(String title, String link, String description, String pubDate, String author, String category) {
  584. this.title = title;
  585. this.link = link;
  586. this.description = description;
  587. this.pubDate = pubDate;
  588. this.author = author;
  589. this.category = category;
  590. }
  591.  
  592. /**
  593. * Set icon
  594. *
  595. * @param icon
  596. */
  597. public void setIcon(Drawable icon) {
  598. mIcon = icon;
  599. }
  600.  
  601. /**
  602. * Get icon
  603. *
  604. * @return
  605. */
  606. public Drawable getIcon() {
  607. return mIcon;
  608. }
  609.  
  610. public String getTitle() {
  611. return title;
  612. }
  613.  
  614. public void setTitle(String title) {
  615. this.title = title;
  616. }
  617.  
  618. public String getLink() {
  619. return link;
  620. }
  621.  
  622. public void setLink(String link) {
  623. this.link = link;
  624. }
  625.  
  626. public String getDescription() {
  627. return description;
  628. }
  629.  
  630. public void setDescription(String description) {
  631. this.description = description;
  632. }
  633.  
  634. public String getPubDate() {
  635. return pubDate;
  636. }
  637.  
  638. public void setPubDate(String pubDate) {
  639. this.pubDate = pubDate;
  640. }
  641.  
  642. public String getAuthor() {
  643. return author;
  644. }
  645.  
  646. public void setAuthor(String author) {
  647. this.author = author;
  648. }
  649.  
  650. public String getCategory() {
  651. return category;
  652. }
  653.  
  654. public void setCategory(String category) {
  655. this.category = category;
  656. }
  657.  
  658. /**
  659. * Compare with the input object
  660. *
  661. * @param other
  662. * @return
  663. */
  664. public int compareTo(RSSNewsItem other) {
  665. if (title.equals(other.getTitle())) {
  666. return -1;
  667. } else if (link.equals(other.getLink())) {
  668. return -1;
  669. } else if (description.equals(other.getDescription())) {
  670. return -1;
  671. } else if (pubDate.equals(other.getPubDate())) {
  672. return -1;
  673. } else if (author.equals(other.getAuthor())) {
  674. return -1;
  675. } else if (category.equals(other.getCategory())) {
  676. return -1;
  677. }
  678.  
  679. return 0;
  680. }
  681.  
  682. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement