xiaosunnihaomang
xiaosunnihaomang
2011-11-13 17:01
浏览 221

3G手机Android应用开发第四天课程2.从网络获取xml格式的视频资讯,我的解析xml出现错误

按照视频中的例子做,出现如下错误:
web项目运行正常,如访问:http://localhost:8088/videoweb/video/list.do
返回的xml为:
<?xml version="1.0" encoding="UTF-8" ?>

  • 喜羊羊与灰太狼全集90
  • 实拍舰载直升东海救援演习20

  • 喀麦隆VS荷兰
    30


    这些短横线不知道是什么东东。
    android项目是想解析这个xml文件,解析器的代码如下:
    public class VideoService {
    public static List getLastVideos() throws Exception{
    String path = "http://192.168.1.100:8088/videoweb/video/list.do";
    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setReadTimeout(5*1000);
    conn.setRequestMethod("GET");
    InputStream inStream = conn.getInputStream();
    byte[] buffer = new byte[1024];
    int len = 0;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    while((len=inStream.read(buffer))!=-1){
    bos.write(buffer, 0, len);
    }
    return parseXML(inStream);
    }

    private static List parseXML(InputStream inStream) throws Exception{
    List videos = null;
    Video video = null;
    XmlPullParser parser = Xml.newPullParser();
    parser.setInput(inStream, "UTF-8");
    int eventType = parser.getEventType();//产生第一个事件
    while(eventType!=XmlPullParser.END_DOCUMENT){//只要不是文档结束事件
    switch (eventType) {
    case XmlPullParser.START_DOCUMENT:
    videos = new ArrayList();
    break;

        case XmlPullParser.START_TAG:
            String name = parser.getName();//获取解析器当前指向的元素的名称
            if("video".equals(name)){
                video = new Video();
                video.setId(new Integer(parser.getAttributeValue(0)));
            }
            if(video!=null){
                if("title".equals(name)){
                    video.setTitle(parser.nextText());//获取解析器当前指向元素的下一个文本节点的值
                }
                if("timelength".equals(name)){
                    video.setTime(new Integer(parser.nextText()));
                }
            }
            break;
        case XmlPullParser.END_TAG:
            if("video".equals(parser.getName())){
                videos.add(video);
                video = null;
            }
            break;
        }
        eventType = parser.next();
    }
    return videos;
    

    }

    }
    而activity代码如下:
    public class VideoClientActivity extends Activity {
    private ListView listView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    listView = (ListView)this.findViewById(R.id.listView);
    try {
        List<Video> videos = VideoService.getLastVideos();
        List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
        for(Video video : videos){
            HashMap<String, Object> item = new HashMap<String, Object>();
            item.put("id", video.getId());
            item.put("title", video.getTitle());
            item.put("timelength", "时长:"+ video.getTime());
            data.add(item);
        }
        Log.i("size", videos.size()+"");
        SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, 
                new String[]{"title", "timelength"}, new int[]{R.id.title, R.id.timelength});
        listView.setAdapter(adapter);
    } catch (Exception e) {
        Toast.makeText(VideoClientActivity.this, "获取最新视频资讯失败", 1).show();
        Log.e("VideoClientActivity", e.toString());
    } 
    

    }
    }
    在LogCat中添加一个过滤器:VideoClientActivity,出现的错误是:
    [color=red]11-13 08:14:21.303: ERROR/VideoClientActivity(365): org.xmlpull.v1.XmlPullParserException: Premature end of document. (position:line

-1, column -1) caused by: org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: no element found[/color]
请各位大侠解救。 :(

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • ccxccx031804
    木兰殇 2011-11-14 15:20
    已采纳

    像<?xml version="1.0" encoding="UTF-8" ?> 这个是版本号和编码格式,xml的编写有一定的格式要求。还有web打开的xml会自动查看你的标签,然后每个小标签出现那种横线。你再仔细看下视频吧,应该介绍得很清楚,不行的话自己多打输出语句,自己调错。

    点赞 评论
  • ccxccx031804
    木兰殇 2011-11-14 10:43

    是解析报错了,你的xml定义可能有问题,再好好看下视频吧

    点赞 评论

相关推荐