我有这个非常奇怪的问题困扰我几天,我检查了我的代码的每个角落,并没有发现任何错误,但我的sax解析器类保持记录错误,我疯了,这有什么问题?有人可以帮助我吗?
MyHandler的
public class MyHandler extends DefaultHandler
{
private boolean in_episode = false;
private boolean in_name = false;
private boolean in_link = false;
private boolean in_desc = false;
private boolean in_date = false;
private List<Episode> li;
private Episode episodes;
private StringBuffer buf=new StringBuffer();
/* 将转换成List<News>的XML数据回传 */
public List<Episode> getParsedData()
{
return li;
}
/* XML文件开始解析时调用此method */
@Override
public void startDocument() throws SAXException
{
li = new ArrayList<Episode>();
}
/* XML文件结束解析时调用此method */
@Override
public void endDocument() throws SAXException
{
}
/* 解析到Element的开头时调用此method */
@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException
{
if (localName.equals("episode"))
{
this.in_episode = true;
/* 解析到item的开头时new一个News对象 */
episodes=new Episode();
}
else if (localName.equals("name"))
{
if(this.in_episode)
{
this.in_name = true;
}
}
else if (localName.equals("link"))
{
if(this.in_episode)
{
this.in_link = true;
}
}
else if (localName.equals("desc"))
{
if(this.in_episode)
{
this.in_desc = true;
}
}
else if (localName.equals("pubdate"))
{
if(this.in_episode)
{
this.in_date = true;
}
}
}
/* 解析到Element的结尾时调用此method */
@Override
public void endElement(String namespaceURI, String localName,
String qName) throws SAXException
{
if (localName.equals("episode"))
{
this.in_episode = false;
/* 解析到item的结尾时将News对象写入List中 */
li.add(episodes);
}
else if (localName.equals("name"))
{
if(this.in_episode)
{
/* 设定News对象的name */
episodes.setname(buf.toString().trim());
buf.setLength(0);
this.in_name = false;
}
}
else if (localName.equals("link"))
{
if(this.in_episode)
{
/* 设定News对象的link */
episodes.setLink(buf.toString().trim());
buf.setLength(0);
this.in_link = false;
}
}
else if (localName.equals("desc"))
{
if(in_episode)
{
/* 设定News对象的description */
episodes.setDesc(buf.toString().trim());
buf.setLength(0);
this.in_desc = false;
}
}
else if (localName.equals("pubdate"))
{
if(in_episode)
{
/* 设定News对象的pubDate */
episodes.setDate(buf.toString().trim());
buf.setLength(0);
this.in_date = false;
}
}
}
/* 取得Element的开头结尾中间夹的字符串 */
@Override
public void characters(char ch[], int start, int length)
{
if(this.in_episode)
{
/* 将char[]加入StringBuffer */
buf.append(ch,start,length);
}
}
}
我的XML文件
<item>
<episode id="1">
<name>1</name>
<link>http://111</link>
<desc>111</desc>
</episode>
<episode id="2">
<name>2</name>
<link>http://222</link>
<desc>222</desc>
</episode>
</item>
MyAdapter
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<Episode> items;
/* MyAdapter的建构子,传入两个参数 */
public MyAdapter(Context context,List<Episode> it)
{
/* 参数初始化 */
mInflater = LayoutInflater.from(context);
items = it;
}
/* 因继承BaseAdapter,需重写以下method */
@Override
public int getCount()
{
return items.size();
}
@Override
public Object getItem(int position)
{
return items.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position,View convertView,ViewGroup par)
{
ViewHolder holder;
if(convertView == null)
{
/* 使用自定义的news_row作为Layout */
convertView = mInflater.inflate(R.layout.episode, null);
/* 初始化holder的text与icon */
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.episode);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Episode tmpN=(Episode)items.get(position);
holder.text.setText(tmpN.getname());
return convertView;
}
/* class ViewHolder */
private class ViewHolder
{
TextView text;
}
}
我的班级称为SaxParser
private List<Episode> getRss(String path)
{
List<Episode> data=new ArrayList<Episode>();
URL url = null;
try
{
url = new URL(path);
Log.d("Showlist", "getRSS path " + path);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
Log.d("Showlist", "Reader ok");
XMLReader xr = sp.getXMLReader();
/* 设定自定义的MyHandler给XMLReader */
MyHandler myExampleHandler = new MyHandler();
xr.setContentHandler(myExampleHandler);
Log.d("Showlist", "Handler attached");
xr.parse(new InputSource(url.openStream()));
Log.d("Showlist", "RSScontent ok");
data =myExampleHandler.getParsedData();
Log.d("Showlist", "DataRead ok");
}
catch (Exception e)
{
Log.e("showlist", "getRSS exception");
}
return data;
}
问题是,我运行程序并且logcat继续记录getRSS异常,listview没有显示任何内容。
12-05 04:47:02.130: INFO/ActivityManager(58): Displayed activity com.appkon.hdtvs/.HDtvs: 10008 ms (total 8324825 ms) 12-05 04:47:02.150: WARN/ActivityManager(58): Activity idle timeout for HistoryRecord{44d867b8 com.appkon.hdtvs/.HDtvs} 12-05 04:47:07.560: DEBUG/dalvikvm(205): GC freed 113 objects / 5056 bytes in 193ms 12-05 04:47:12.560: DEBUG/dalvikvm(103): GC freed 2698 objects / 155704 bytes in 241ms 12-05 04:47:17.060: INFO/HDtvs(345): path = http://appkon.com/hdtvs/channel/bigbang2.xml 12-05 04:47:17.081: INFO/ActivityManager(58): Starting activity: Intent { cmp=com.appkon.hdtvs/.Showlist (has extras) } 12-05 04:47:17.501: DEBUG/Showlist(345): getRSS path http://appkon.com/hdtvs/channel/bigbang2.xml 12-05 04:47:17.510: DEBUG/Showlist(345): Reader ok 12-05 04:47:17.520: DEBUG/Showlist(345): Handler attached 12-05 04:47:19.700: ERROR/showlist(345): getRSS exception 12-05 04:47:19.700: INFO/Showlist(345): path = http://appkon.com/hdtvs/channel/bigbang2.xml 12-05 04:47:19.730: INFO/showlist(345): dblink = http://movie.douban.com/subject/3190880/ 12-05 04:47:20.620: INFO/ActivityManager(58): Displayed activity com.appkon.hdtvs/.Showlist: 3451 ms (total 3451 ms)
答案 0 :(得分:1)
以便在调试期间获得帮助:
您需要打印确切的堆栈跟踪以了解正在进行的操作
Log.e("YourApp", "getRSS exception" + e );
或
Log.e("YourApp", "getRSS exception" + e.printStackTrace() );
至于您所面临的问题,可能是因为您尝试阅读的xml文件有404-ed