@SuppressLint("NewApi")
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 static Bitmap image = null;
private String imageUrl=null;
private static String title = null;
private static StringBuffer description = new StringBuffer();
private static String date = null;
public void processFeed() {
try {
//This part is added to allow the network connection on a main GUI thread...
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setContentHandler(this);
URL urlObj = new URL(url);
InputStream inputStream = urlObj.openConnection().getInputStream();
reader.parse(new InputSource(inputStream));
}
catch (Exception e)
{
e.printStackTrace();
System.out.println(new String("Got Exception General"));
}
}
private Bitmap getBitmap(String url) {
try {
System.out.println(url);
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)
{
System.out.println(new String("IOException in reading Image"));
return null;
}
catch (Exception ioe)
{
System.out.println(new String("IOException GENERAL"));
return null;
}
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
{
if (localName.equals("enclosure"))
{
System.out.println(new String("characters Image"));
imageUrl = attributes.putValue("","url");
System.out.println(imageUrl);
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) {
System.out.println(new String("characters"));
String chars = new String(ch).substring(start, start + length);
System.out.println(chars);
if (inUrl && image == null)
{
System.out.println(new String("IMAGE"));
System.out.println(imageUrl);
image = getBitmap(imageUrl);
}
if (inTitle && title == null) {
System.out.println(new String("TITLE"));
title = chars; }
if (inDescription) { description.append(chars); }
if (inDate && date == null) { date = chars; }
}
public static Bitmap getImage() { return image; }
public static String getTitle() { return title; }
public static StringBuffer getDescription() { return description; }
public static String getDate() { return date; }
}
这是我的MainActivity类我试图从nasa获取数据rss feed所有数据都显示在logcat中但未显示在模拟器中
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IotdHandler handler = new IotdHandler(); //create handler
handler.processFeed(); //start parsing
resetDisplay(IotdHandler.getTitle(), IotdHandler.getDate(),
IotdHandler.getImage(), IotdHandler.getDescription());
}
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);
}
}
答案 0 :(得分:0)
包含属性的类:
public class ImageDetails {
private String imageDate, imageTitle, imageDesc;
// getters and setters here
}
在IOHandler类中,在startElement函数内添加,
if(qName.equalsIgnoreCase("channel") {
imageDetails = new ImageDetails();
// make sure imageDetails is declared in the class
}
此行在找到开始标记通道时实例化imgedetails类,并且由于Feed只有一个项目,因此它只会被实例化一次。
在IOHandler类中进行的其他更改
创建一个像
这样的函数public ImageDetails getImageDetails() {
// call the function that reads the documents or parses it
return imageDetails;
}
每当localnames匹配时,使用setter方法设置值。
在活动类
中创建IOHandler的实例IOHandler ioHandler = new IOHandler(pass values if needed);
ImageDetails imageDetails = ioHandler.getImageDetails();
使用imageDetails.getter方法用数据填充textviews。
====== 编辑#1
将startElement函数更改为:
@Override
public void startElement(String uri, String localName, String qName,
org.xml.sax.Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
if (qName.equals("enclosure"))
{
System.out.println(new String("characters Image"));
//imageUrl = attributes.putValue("","url");
// I have commented out this line, try to figure out how to get this atleast
System.out.println(imageUrl);
inUrl = true;
}
else { inUrl = false; }
if (qName.startsWith("item")) { inItem = true; }
else if (inItem) {
if (qName.equals("title")) { inTitle = true; }
else { inTitle = false; }
if (qName.equals("description")) { inDescription = true; }
else { inDescription = false; }
if (qName.equals("pubDate")) { inDate = true; }
else { inDate = false; }
}
}
}
希望这有帮助。