Android 使用Sax解析从Web服务器获取数据 我使用SAX解析方法从Web服务器获取数据所有数据都显示在LogCat中,但没有显示在我的模拟器中
这是我的MainActivity类,我正在尝试从nasa rss提要获取数据。所有数据都显示在logcat中,但没有显示在模拟器中Android 使用Sax解析从Web服务器获取数据 我使用SAX解析方法从Web服务器获取数据所有数据都显示在LogCat中,但没有显示在我的模拟器中,android,parsing,sax,Android,Parsing,Sax,这是我的MainActivity类,我正在尝试从nasa rss提要获取数据。所有数据都显示在logcat中,但没有显示在模拟器中 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(
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);
}
}
包含以下属性的类:
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类,因为提要只有一个项,所以只会提示一次
要在IOHandler类中进行的其他更改
创建一个函数,如
public ImageDetails getImageDetails() {
// call the function that reads the documents or parses it
return imageDetails;
}
只要本地名称匹配,就使用setter方法来设置值
在activity类中创建IOHandler的实例
IOHandler ioHandler = new IOHandler(pass values if needed);
ImageDetails imageDetails = ioHandler.getImageDetails();
使用imageDetails.getter方法用数据填充文本视图
========
编辑#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; }
}
}
}
希望这有帮助。为什么不创建一个具有属性的类,然后将其作为返回类型接收?不要在代码中使用静态引用?也尝试调试它,以了解哪里出了问题我是一个初学者。你能解释一下如何创建带有属性的类并将其作为返回类型接收吗?它不适合我。你能帮我更正代码吗。这将有助于我更好地理解解析。嗨,请仔细阅读并更改localname与qName之间的比较,我的意思是使用qName而不是localname。我不介意为您做这项工作,但在这种情况下,您将无法自学。试着这样做,如果你有问题就回来。祝你好运此外,由于您使用的是android,请停止使用sysout。使用日志
@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; }
}
}
}