Android开发之获取web服务器xml数据

本文介绍了一种在Android应用中从Web服务器获取XML数据的方法,并通过Pull解析将其数据显示在ListView上。具体步骤包括配置J2EE环境、创建业务Bean类、实现数据解析逻辑等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Android开发之获取web服务器xml数据

首先我们需要配置J2EE开发环境,并部署web应用viderweb,启动服务

然后开始我们的程序代码

我们的程序大致是获取web服务器xmlàpull解析xmlàListView列表显示数据

添加业务bean类Video

package cn.class3g.domain;

 

public class Video {

   private int id;

   private String title;

   private int timeLenght;

 

   public Video() {

      super();

      // TODO Auto-generated constructor stub

   }

 

   public Video(int id, String title, int timeLenght) {

      super();

      this.id = id;

      this.title = title;

      this.timeLenght = timeLenght;

   }

 

   public int getId() {

      return id;

   }

 

   public void setId(int id) {

      this.id = id;

   }

 

   public String getTitle() {

      return title;

   }

 

   public void setTitle(String title) {

      this.title = title;

   }

 

   public int getTimeLenght() {

      return timeLenght;

   }

 

   public void setTimeLenght(int timeLenght) {

      this.timeLenght = timeLenght;

   }

 

   @Override

   public String toString() {

      return "Video [id=" + id + ", title=" + title + ", timeLenght="

           + timeLenght + "]";

   }

 

}


布局

main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

 

    <ListView

        android:id="@+id/listViewID"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content" >

    </ListView>

 

</LinearLayout>


Item.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="horizontal" >

 

    <TextView

        android:id="@+id/titleID"

        android:layout_width="200dp"

        android:layout_height="wrap_content" />

 

    <TextView

        android:id="@+id/timelengthID"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content" />

 

</LinearLayout>


添加业务类VideoService类

package cn.class3g.service;

 

import java.io.InputStream;

import java.net.HttpURLConnection;

 

import java.net.URL;

import java.util.ArrayList;

import java.util.List;

 

import org.xmlpull.v1.XmlPullParser;

 

import android.util.Xml;

 

import cn.class3g.domain.Video;

 

public class VideoService {

   public static List<Video> getLastVideos() throws Exception {

      String path = "http://192.168.65.2:8080/videoweb/video/list.do";

      URL url = new URL(path);

 

      HttpURLConnection conn = (HttpURLConnection) url.openConnection();

      conn.setConnectTimeout(5000);

      conn.setRequestMethod("GET");

      InputStream inStream = conn.getInputStream();

 

      return parseXML(inStream);

 

   }

 

   private static List<Video> parseXML(InputStream inStream) throws Exception {

 

      XmlPullParser parser = Xml.newPullParser();

      parser.setInput(inStream, "UTF-8");

 

      int event = parser.getEventType();

 

      List<Video> videos = null;

 

      Video video = null;

 

      while (event != XmlPullParser.END_DOCUMENT) {

        switch (event) {

 

        case XmlPullParser.START_DOCUMENT:

           videos = new ArrayList<Video>();

           break;

        case XmlPullParser.START_TAG:

           if ("video".equals(parser.getName())) {

              video = new Video();

              int id = Integer.valueOf(parser.getAttributeValue(0));

              video.setId(id);

           } else if ("title".equals(parser.getName()) && video != null) {

              video.setTitle(parser.nextText());

           } else if ("timelength".equals(parser.getName())

                 && video != null) {

              video.setTimeLenght(Integer.valueOf(parser.nextText()));

           }

           break;

        case XmlPullParser.END_TAG:

           if ("video".equals(parser.getName()) && video != null) {

              videos.add(video);

              video = null;

           }

           break;

        }

 

        event = parser.next();

      }

      return videos;

   }

}


VideoListActivity

package cn.class3g.video;

 

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

 

import cn.class3g.domain.Video;

import cn.class3g.service.VideoService;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.widget.ListView;

import android.widget.SimpleAdapter;

import android.widget.Toast;

 

public class VideoListActivity extends Activity {

   ListView videolv;

 

   public void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.main);

      try {

        findViews();

      } catch (Exception e) {

        // TODO Auto-generated catch block

        Log.e("tag", e.toString());

        // Toast.makeText(this, text, duration)

      }

   }

 

   private void findViews() throws Exception {

      videolv = (ListView) this.findViewById(R.id.listViewID);

 

      ArrayList<Video> videos = (ArrayList<Video>) VideoService

           .getLastVideos();

 

      List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();

 

      for (Video v : videos) {

        HashMap<String, Object> item = new HashMap<String, Object>();

        item.put("title", v.getTitle());

        item.put("timelenght", "时长“:" + v.getTimeLenght() + "分钟");

        item.put("id", v.getId());

        data.add(item);

      }

      SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,

           new String[] { "title", "timelenght" }, new int[] {

                 R.id.titleID, R.id.timelengthID });

      videolv.setAdapter(adapter);

   }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值