java - Android 头显 "NASA daily image App"

标签 java android eclipse rss

我一直在关注《Head First Android》这本书,但我陷入了第 3 章。

这个小应用程序是一个非常基本的布局; 3 个 TextView 和 1 个 ImageView ,在阅读 NASA RSS 每日图像后应该更新。

我已经完成了本章,但现在运行应用程序时仅显示空白屏幕。

任何帮助表示赞赏。这是代码:

public class MainActivity extends Activity {

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IotdHandler handler = new IotdHandler ();
        handler.processFeed();
        resetDisplay (handler.getTitle(), handler.getDate(), handler.getImage(), handler.getDescription());
    }

    public class IotdHandler extends DefaultHandler {
        private String url = "http://www.nasa.gov/rss/dyn/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 void processFeed() {
            try {
            SAXParserFactory factory =
            SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            XMLReader reader = parser.getXMLReader();
            reader.setContentHandler(this);
            InputStream inputStream = new URL(url).openStream();
            reader.parse(new InputSource(inputStream));
            } catch (Exception e) {  }
        }

            private Bitmap getBitmap(String url) {
                try {
                HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                Bitmap bilde = BitmapFactory.decodeStream(input);
                input.close();
                return bilde;
                } catch (IOException ioe) { return null; }
                }

            public void startElement(String url, String localName, String qName, Attributes attributes) throws SAXException {
                    if (localName.endsWith(".jpg")) { 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 (inUrl && url == null) { image = getBitmap(chars); }
                if (inTitle && title == null) { title = chars; }
                if (inDescription) { description.append(chars); }
                if (inDate && date == null) { date = chars; }
         }

        public Bitmap getImage() { return image; }
        public String getTitle() { return title; }
        public StringBuffer getDescription() { return description; }
        public String getDate() { return date; }
}

    private void resetDisplay (String title, String date, Bitmap image, StringBuffer description) {

        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);
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   // Inflate the menu; this adds items to the action bar if it is present.
   getMenuInflater().inflate(R.menu.main, menu);
   return true;
}
}

最佳答案

您应该使用 AsyncTask 作为内部类。

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    IotdHandler handler = new IotdHandler ();
    new MyTask().execute();
}

然后在doInBackground()中解析文档并在onPostExecute()中调用resetDisplay。

public class MyTask extends AsyncTask<Void, Void, Void>{

@Override
protected Void doInBackground(Void... params) {
    handler.processFeed();
    return null;
}

@Override
protected void onPostExecute(Void result) {
    resetDisplay (handler.getTitle(), handler.getDate(), handler.getImage(), handler.getDescription());
    super.onPostExecute(result);
}
}

有关如何传递参数、返回结果等的更多信息。 AsyncTask Document

关于java - Android 头显 "NASA daily image App",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20897456/

相关文章:

java - BIRT 中的联合数据集仅限于两个表

eclipse - 如何完全删除Subclipse插件?

java - Jdbc 检查功能 - 保存点释放

java - 在 Eclipse IDE 中通过文件获取用户输入到 java 程序

java - 如何在编辑文本字段上设置单词java(android)

android - 无法在 CirclePageIndicator 上设置 CurrentItem(0)

java String 非原始类型的泛型类

Java-将多维数组映射到单维数组

android - Vector Drawables 在 Android 4+ 上开箱即用

eclipse - Grails 项目结构中的文件夹未显示