Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package id.mediatutorial.nasadailyimages;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.AsyncTask;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.ImageView;
- import android.widget.TextView;
- import org.xml.sax.Attributes;
- import org.xml.sax.InputSource;
- import org.xml.sax.SAXException;
- import org.xml.sax.XMLReader;
- import org.xml.sax.helpers.DefaultHandler;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import javax.xml.parsers.SAXParser;
- import javax.xml.parsers.SAXParserFactory;
- public class MainActivity extends AppCompatActivity {
- IotdHandler handler = new IotdHandler();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- /****** IotdHandler *****/
- handler.processFeed();
- }
- public void ResetDisplay() {
- String title = handler.getTitle();
- String date = handler.getDate();
- String description = handler.getDescription().toString();
- resetDisplay(title, date, handler.getImage(), description);
- }
- private void resetDisplay(String title, String date, Bitmap image, String description) {
- try {
- TextView titleView = (TextView) findViewById(R.id.imageTitle);
- titleView.setText(title);
- TextView dateView = (TextView) findViewById(R.id.imageDate);
- dateView.setText(date);
- ImageView imageView = (ImageView) findViewById(R.id.imageDisplay);
- imageView.setImageBitmap(image);
- TextView descriptionView = (TextView) findViewById(R.id.imageDescription);
- descriptionView.setText(description);
- }catch(Exception e){
- TextView titleView = (TextView) findViewById(R.id.imageTitle);
- titleView.setText(e.toString());
- }
- }
- public class IotdHandler extends DefaultHandler {
- private String url = "http://www.nasa.gov/rss/image_of_the_day.rss";
- private boolean inUrl = false;
- private boolean inTitle = false;
- private boolean inDescription = false;
- private boolean inItem = false;
- private boolean inDate = false;
- private Bitmap image = null;
- private String title = null;
- private StringBuffer description = new StringBuffer();
- private String date = null;
- public XMLReader reader = null;
- private Bitmap getBitmap(String url) {
- try {
- HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
- connection.setDoInput(true);
- connection.connect();
- InputStream input = connection.getInputStream();
- Bitmap bitmap = BitmapFactory.decodeStream(input);
- input.close();
- return bitmap;
- } catch (IOException ioe) {
- TextView titleView = (TextView) findViewById(R.id.imageTitle);
- titleView.setText(ioe.toString());
- return null;
- }
- }
- public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
- if (localName.equals("enclosure")) {
- inUrl = true;
- } else {
- inUrl = false;
- }
- if (localName.startsWith("item")) {
- inItem = true;
- } else if (inItem) {
- if (localName.equals("title")) {
- inTitle = true;
- } else {
- inTitle = false;
- }
- if (localName.equals("description")) {
- inDescription = true;
- } else {
- inDescription = false;
- }
- if (localName.equals("pubDate")) {
- inDate = true;
- } else {
- inDate = false;
- }
- }
- }
- public void characters(char ch[], int start, int length) {
- String chars = new String(ch).substring(start, start + length);
- if (inTitle && title == null) {
- title = chars;
- }
- if (inDescription) {
- description.append(chars);
- }
- if (inDate && date == null) {
- date = chars;
- }
- }
- private class ProcessFeedTask extends AsyncTask<String, Void, InputStream> {
- @Override
- protected InputStream doInBackground(String... params) {
- String url = params[0];
- InputStream inputStream = null;
- try {
- inputStream = new URL(url).openStream();
- reader.parse(new InputSource(inputStream));
- } catch (Exception e) {
- //TextView titleView = (TextView) findViewById(R.id.imageTitle);
- //titleView.setText(e.toString());
- //
- //i comment out two line above becose it always display error:
- // //method setText must be called from UI Thread
- }
- return inputStream;
- }
- @Override
- protected void onPostExecute(InputStream result) {
- super.onPostExecute(result);
- if (result != null) {
- ResetDisplay();
- }
- }
- }
- public void processFeed()
- {
- try
- {
- SAXParserFactory factory = SAXParserFactory.newInstance();
- SAXParser parser = factory.newSAXParser();
- reader = parser.getXMLReader();
- reader.setContentHandler(this);
- new ProcessFeedTask().execute(url);
- }
- catch (Exception e)
- {
- TextView titleView = (TextView) findViewById(R.id.imageTitle);
- titleView.setText(e.toString());
- }
- }
- public Bitmap getImage() {
- return image;
- }
- public String getTitle() {
- return title;
- }
- public String getDescription() {
- return description.toString();
- }
- public String getDate() {
- return date;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement