怎么才能把WEB端的XML接受到ANDROID 需要怎么开启线程

硕硕 发布于 2013/02/04 16:51
阅读 274
收藏 1

@gaojie 你好,想跟你请教个问题:

WEB端已经解决问题----------------------------------------------------

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

- < videonews >
- < news id =" 35 " >
  < title > 喜欢1 </ title >
  < timelength > 90 </ timelength >
  </ news >
- < news id =" 12 " >
  < title > 喜欢2 </ title >
  < timelength > 20 </ timelength >
  </ news >
- < news id =" 86 " >
  < title > 喜欢3 </ title >
  < timelength > 90 </ timelength >
  </ news >
  </ videonews >
 

android端---------------------------------------------------

public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ListView listView = (ListView) this.findViewById(R.id.listView);
 try
 {
  List<News> videos= VideoNewsService.getLastNews();
  
  List<HashMap<String,Object>> data = new ArrayList<HashMap<String, Object>>();

  for(News news : videos)
  {
 HashMap<String,Object> item =new  HashMap<String, Object>(); 
 item.put("id", news.getId());
 item.put("title", news.getTitle());
 item.put("timelength", news.gettimeiength());
 data.add(item);
 }
 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)
 {
  e.printStackTrace();
 }
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}

------------------------------------

public class VideoNewsService {
 public static List<News> getLastNews() throws Exception {

  String path = "http://192.168.1.101:8080/videonews/ListServlet";
  URL url = new URL(path);
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setConnectTimeout(5 * 1000);
  conn.setRequestMethod("GET");
  if (conn.getResponseCode() == 200) {
   InputStream inStrem = conn.getInputStream();
   return parseXML(inStrem);

  }
  return null;

 }
 public static List<News> parseXML(InputStream inStrem) throws Exception {
  List<News> newses = new ArrayList<News>();
  News news = null;
  XmlPullParser parser = Xml.newPullParser();
  parser.setInput(inStrem, "UTF-8");
  int event = parser.getEventType();
  while (event != XmlPullParser.END_DOCUMENT) {
   switch (event) {
   case XmlPullParser.START_TAG:
    if ("news".equals(parser.getName())) {
     int id = new Integer(parser.getAttributeValue(0));
     news = new News();
     news.setId(id);
    } else if ("tltle".equals(parser.getName())) {
     news.setTitle(parser.nextText());
    } else if ("timelength".equals(parser.getName())) {
     news.settimeiength(new Integer(parser.nextText()));
    }
    break;
   case XmlPullParser.END_TAG:
    if ("news".equals(parser.getName())) {
     newses.add(news);
     news = null;
    }
    break;
   }
   event = parser.next();
  }
  return newses;
 }
}

--------------------------------------------------

public class News {
        private Integer id;
        private String title;
        private Integer timeiength;
        public News(){}
        public News(Integer id, String title, Integer timeiength) {
   super();
   this.id = id;
   this.title = title;
   this.timeiength = timeiength;
  }
  public Integer getId() {
   return id;
  }
  public void setId(Integer id) {
   this.id = id;
  }
  public String getTitle() {
   return title;
  }
  public void setTitle(String title) {
   this.title = title;
  }
  public Integer gettimeiength() {
   return timeiength;
  }
  public void settimeiength(Integer timeiength) {
   this.timeiength = timeiength;
  }
  
}

 

加载中
0
Joy1024
Joy1024
我还是不是很明白你的意思? 你的xml已经可以显示了, 你还想要什么?
0
硕硕

你好 我在ANDROID 上写了上面的代码 显示不出来 模礼器是空白的 数据没有收到 02-04 12:46:02.882: D/gralloc_goldfish(972): Emulator without GPU emulation detected.
02-04 12:46:01.902: E/Trace(972): error opening trace file: No such file or directory (2)
02-04 12:46:02.582: W/System.err(972): android.os.NetworkOnMainThreadException
02-04 12:46:02.582: W/System.err(972):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)

 

返回顶部
顶部