gpt4 book ai didi

java - Android 先入为主 "NASA daily image App"

转载 作者:搜寻专家 更新时间:2023-11-01 08:53:37 24 4
gpt4 key购买 nike

我一直在关注“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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com