Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android訪問web工程,並解析web工程返回的xml文件

android訪問web工程,並解析web工程返回的xml文件

編輯:關於Android編程

如題:

首先建立一個簡單的web工程,使用servlet技術:

下面是servlet的實現。


[html] 
/** 
 * @FILE:ListServlet.java 
 * @AUTHOR:Administrator 
 * @DATE:2013-5-19 下午6:03:19 
 **/ 
package com.yehui.servlet; 
 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.List; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import com.yehui.service.VideoNewsService; 
import com.yehui.service.bean.News; 
import com.yehui.service.impl.VideoNewsServiceImpl; 
 
/******************************************* 
 *  
 * @CLASS:ListServlet 
 * @DESCRIPTION: 
 * @AUTHOR:Administrator 
 * @VERSION:v1.0 
 * @DATE:2013-5-19 下午6:03:19 
 *******************************************/ 
public class ListServlet extends HttpServlet { 
    private VideoNewsService service = new VideoNewsServiceImpl(); 
 
    /** 
     * The doGet method of the servlet. <br> 
     *  
     * This method is called when a form has its tag value method equals to get. 
     *  
     * @param request 
     *            the request send by the client to the server 
     * @param response 
     *            the response send by the server to the client 
     * @throws ServletException 
     *             if an error occurred 
     * @throws IOException 
     *             if an error occurred 
     */ 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        doPost(request, response); 
 
    } 
 
    /** 
     * The doPost method of the servlet. <br> 
     *  
     * This method is called when a form has its tag value method equals to 
     * post. 
     *  
     * @param request 
     *            the request send by the client to the server 
     * @param response 
     *            the response send by the server to the client 
     * @throws ServletException 
     *             if an error occurred 
     * @throws IOException 
     *             if an error occurred 
     */ 
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        List<News> videos = service.getLastNews(); 
        request.setAttribute("videos", videos); 
        // ("")裡面是jsp的文件路徑 
        request.getRequestDispatcher("/WEB-INF/page/videonews.jsp").forward(request, response); 
    } 
 

/**
 * @FILE:ListServlet.java
 * @AUTHOR:Administrator
 * @DATE:2013-5-19 下午6:03:19
 **/
package com.yehui.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yehui.service.VideoNewsService;
import com.yehui.service.bean.News;
import com.yehui.service.impl.VideoNewsServiceImpl;

/*******************************************
 *
 * @CLASS:ListServlet
 * @DESCRIPTION:
 * @AUTHOR:Administrator
 * @VERSION:v1.0
 * @DATE:2013-5-19 下午6:03:19
 *******************************************/
public class ListServlet extends HttpServlet {
 private VideoNewsService service = new VideoNewsServiceImpl();

 /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request
  *            the request send by the client to the server
  * @param response
  *            the response send by the server to the client
  * @throws ServletException
  *             if an error occurred
  * @throws IOException
  *             if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doPost(request, response);

 }

 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to
  * post.
  *
  * @param request
  *            the request send by the client to the server
  * @param response
  *            the response send by the server to the client
  * @throws ServletException
  *             if an error occurred
  * @throws IOException
  *             if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  List<News> videos = service.getLastNews();
  request.setAttribute("videos", videos);
  // ("")裡面是jsp的文件路徑
  request.getRequestDispatcher("/WEB-INF/page/videonews.jsp").forward(request, response);
 }

}
web的service操作的bean:News:

 

[html] 
/** 
 * @FILE:News.java 
 * @AUTHOR:Administrator 
 * @DATE:2013-5-19 下午6:08:00 
 **/ 
package com.yehui.service.bean; 
 
/******************************************* 
 *  
 * @CLASS:News 
 * @DESCRIPTION: 
 * @AUTHOR:Administrator 
 * @VERSION:v1.0 
 * @DATE:2013-5-19 下午6:08:00 
 *******************************************/ 
public class News { 
    private Integer id; 
    private String title; 
    private Integer timelength; 
 
    public News() { 
    } 
 
    public News(Integer id, String title, Integer timelength) { 
        this.id = id; 
        this.title = title; 
        this.timelength = timelength; 
    } 
 
    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 getTimelength() { 
        return timelength; 
    } 
 
    public void setTimelength(Integer timelength) { 
        this.timelength = timelength; 
    } 
 

/**
 * @FILE:News.java
 * @AUTHOR:Administrator
 * @DATE:2013-5-19 下午6:08:00
 **/
package com.yehui.service.bean;

/*******************************************
 *
 * @CLASS:News
 * @DESCRIPTION:
 * @AUTHOR:Administrator
 * @VERSION:v1.0
 * @DATE:2013-5-19 下午6:08:00
 *******************************************/
public class News {
 private Integer id;
 private String title;
 private Integer timelength;

 public News() {
 }

 public News(Integer id, String title, Integer timelength) {
  this.id = id;
  this.title = title;
  this.timelength = timelength;
 }

 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 getTimelength() {
  return timelength;
 }

 public void setTimelength(Integer timelength) {
  this.timelength = timelength;
 }

}

web的service接口:


[html] 
/** 
 * @FILE:VideoNewsService.java 
 * @AUTHOR:Administrator 
 * @DATE:2013-5-19 下午6:14:18 
 **/ 
package com.yehui.service; 
 
import java.util.List; 
 
import com.yehui.service.bean.News; 
 
/******************************************* 
 *  
 * @CLASS:VideoNewsService 
 * @DESCRIPTION:     
 * @AUTHOR:Administrator 
 * @VERSION:v1.0 
 * @DATE:2013-5-19 下午6:14:18 
 *******************************************/ 
public interface VideoNewsService { 
 
    /**@description:獲取最新的視頻資訊    
     * @author:Administrator 
     * @return:List<News> 
     */ 
     
    public List<News> getLastNews(); 
 

/**
 * @FILE:VideoNewsService.java
 * @AUTHOR:Administrator
 * @DATE:2013-5-19 下午6:14:18
 **/
package com.yehui.service;

import java.util.List;

import com.yehui.service.bean.News;

/*******************************************
 *
 * @CLASS:VideoNewsService
 * @DESCRIPTION: 
 * @AUTHOR:Administrator
 * @VERSION:v1.0
 * @DATE:2013-5-19 下午6:14:18
 *******************************************/
public interface VideoNewsService {

 /**@description:獲取最新的視頻資訊 
  * @author:Administrator
  * @return:List<News>
  */
 
 public List<News> getLastNews();

}

service的實現:


[html] 
/** 
 * @FILE:VideoNewsServiceImpl.java 
 * @AUTHOR:Administrator 
 * @DATE:2013-5-19 下午6:06:33 
 **/ 
package com.yehui.service.impl; 
 
import java.util.ArrayList; 
import java.util.List; 
 
import com.yehui.service.VideoNewsService; 
import com.yehui.service.bean.News; 
 
/******************************************* 
 *  
 * @CLASS:VideoNewsServiceImpl 
 * @DESCRIPTION:     
 * @AUTHOR:Administrator 
 * @VERSION:v1.0 
 * @DATE:2013-5-19 下午6:06:33 
 *******************************************/ 
public class VideoNewsServiceImpl implements VideoNewsService { 
public List<News> getLastNews(){ 
    List<News> newes=new ArrayList<News>(); 
    newes.add(new News(1,"喜洋洋",90)); 
    newes.add(new News(2,"灰太狼",30)); 
    newes.add(new News(3,"泷澤蘿拉",10)); 
    return newes; 

/**
 * @FILE:VideoNewsServiceImpl.java
 * @AUTHOR:Administrator
 * @DATE:2013-5-19 下午6:06:33
 **/
package com.yehui.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.yehui.service.VideoNewsService;
import com.yehui.service.bean.News;

/*******************************************
 *
 * @CLASS:VideoNewsServiceImpl
 * @DESCRIPTION: 
 * @AUTHOR:Administrator
 * @VERSION:v1.0
 * @DATE:2013-5-19 下午6:06:33
 *******************************************/
public class VideoNewsServiceImpl implements VideoNewsService {
public List<News> getLastNews(){
 List<News> newes=new ArrayList<News>();
 newes.add(new News(1,"喜洋洋",90));
 newes.add(new News(2,"灰太狼",30));
 newes.add(new News(3,"泷澤蘿拉",10));
 return newes;
}
}

jsp文件,這裡注意是要返回xml的結果:所以文件的第一行必須加上<?xml version="1.0" encoding="UTF-8"?>


[html] 
<%@ page language="java" contentType="text/xml; charset=UTF-8" import="java.util.*" pageEncoding="utf-8"%><?xml version="1.0" encoding="UTF-8"?> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> 
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> 
<videonews > 
    <c:forEach items="${videos}" var="video"  > 
    <news id="${video.id}"> 
    <title>${video.title}</title> 
    <timelength>${video.timelength}</timelength> 
    </news> 
    </c:forEach> 
</videonews> 

<%@ page language="java" contentType="text/xml; charset=UTF-8" import="java.util.*" pageEncoding="utf-8"%><?xml version="1.0" encoding="UTF-8"?>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<videonews >
    <c:forEach items="${videos}" var="video"  >
 <news id="${video.id}">
 <title>${video.title}</title>
 <timelength>${video.timelength}</timelength>
 </news>
 </c:forEach>
</videonews>

web.xml文件配置servlet:


[html]
<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0"  
    xmlns="http://java.sun.com/xml/ns/javaee"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
  <display-name></display-name> 
  <servlet> 
    <description>This is the description of my J2EE component</description> 
    <display-name>This is the display name of my J2EE component</display-name> 
    <servlet-name>ListServlet</servlet-name> 
    <servlet-class>com.yehui.servlet.ListServlet</servlet-class> 
  </servlet> 
 
  <servlet-mapping> 
    <servlet-name>ListServlet</servlet-name> 
    <url-pattern>/ListServlet</url-pattern> 
  </servlet-mapping>   
  <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list> 
</web-app> 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>ListServlet</servlet-name>
    <servlet-class>com.yehui.servlet.ListServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>ListServlet</servlet-name>
    <url-pattern>/ListServlet</url-pattern>
  </servlet-mapping> 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
好了,web服務器端已經構建成功。下面介紹android客戶端:

依然為service操作的bean:


[html] 
/** 
 * @FILE:News.java 
 * @AUTHOR:Administrator 
 * @DATE:2013-5-19 下午6:08:00 
 **/ 
package com.yehui.bean; 
 
/******************************************* 
 *  
 * @CLASS:News 
 * @DESCRIPTION: 
 * @AUTHOR:Administrator 
 * @VERSION:v1.0 
 * @DATE:2013-5-19 下午6:08:00 
 *******************************************/ 
public class News { 
    private Integer id; 
    private String title; 
    private Integer timelength; 
 
    public News() { 
    } 
 
    public News(Integer id, String title, Integer timelength) { 
        this.id = id; 
        this.title = title; 
        this.timelength = timelength; 
    } 
 
    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 getTimelength() { 
        return timelength; 
    } 
 
    public void setTimelength(Integer timelength) { 
        this.timelength = timelength; 
    } 
 

/**
 * @FILE:News.java
 * @AUTHOR:Administrator
 * @DATE:2013-5-19 下午6:08:00
 **/
package com.yehui.bean;

/*******************************************
 *
 * @CLASS:News
 * @DESCRIPTION:
 * @AUTHOR:Administrator
 * @VERSION:v1.0
 * @DATE:2013-5-19 下午6:08:00
 *******************************************/
public class News {
 private Integer id;
 private String title;
 private Integer timelength;

 public News() {
 }

 public News(Integer id, String title, Integer timelength) {
  this.id = id;
  this.title = title;
  this.timelength = timelength;
 }

 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 getTimelength() {
  return timelength;
 }

 public void setTimelength(Integer timelength) {
  this.timelength = timelength;
 }

}

service:通過pull技術對返回的xml文件內容進行解析:


[html]
/** 
 * @FILE:VideoNewsService.java 
 * @AUTHOR:Administrator 
 * @DATE:2013-5-19 下午8:29:22 
 **/ 
package com.yehui.service; 
 
import java.io.IOException; 
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 com.yehui.bean.News; 
 
/******************************************* 
 *  
 * @CLASS:VideoNewsService 
 * @DESCRIPTION: 
 * @AUTHOR:Administrator 
 * @VERSION:v1.0 
 * @DATE:2013-5-19 下午8:29:22 
 *******************************************/ 
public class VideoNewsService { 
    public static List<News> getLastNews() throws Exception { 
        List<News> newes = new ArrayList<News>(); 
        URL url = new URL("http://169.254.161.54:8888/web/ListServlet"); 
        HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
        conn.setRequestMethod("GET"); 
        conn.setConnectTimeout(5000); 
        conn.connect(); 
        if (conn.getResponseCode() == 200) { 
            InputStream inputStream = conn.getInputStream(); 
            // 因為頁面返回值是xml文件,所以現在就要解析這個xml,使用pull解析器解析 
             
            newes= parseXML(inputStream); 
 
        } 
        return newes; 
    } 
 
    /** 
     * @description:pull解析服務器返回xml文件 
     * @author:Administrator 
     * @return:List<News> 
     * @param inputStream 
     * @return 
     * @throws XmlPullParserException 
     * @throws IOException 
     */ 
 
    private static List<News> parseXML(InputStream inputStream) 
            throws Exception { 
        XmlPullParser xmlPullParser = Xml.newPullParser(); 
        xmlPullParser.setInput(inputStream, "utf-8"); 
        int event = xmlPullParser.getEventType(); 
        List<News> newes = new ArrayList<News>(); 
        News news = null; 
        while (event != xmlPullParser.END_DOCUMENT) { 
            switch (event) { 
            case 2: 
                if ("news".equals(xmlPullParser.getName())) { 
                    int id = Integer 
                            .valueOf(xmlPullParser.getAttributeValue(0)); 
                    news = new News(); 
                    news.setId(id); 
                } else if ("title".equals(xmlPullParser.getName())) { 
                    news.setTitle(xmlPullParser.nextText()); 
                } else if ("timelength".equals(xmlPullParser.getName())) { 
                    news.setTimelength(Integer.valueOf(xmlPullParser.nextText())); 
                } 
                break; 
            case 3: 
                if ("news".equals(xmlPullParser.getName())) { 
                    newes.add(news); 
                    news = null; 
                } 
                break; 
                default: 
                    break; 
                     
            } 
             
            event = xmlPullParser.next(); 
        } 
        return newes; 
    } 

/**
 * @FILE:VideoNewsService.java
 * @AUTHOR:Administrator
 * @DATE:2013-5-19 下午8:29:22
 **/
package com.yehui.service;

import java.io.IOException;
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 com.yehui.bean.News;

/*******************************************
 *
 * @CLASS:VideoNewsService
 * @DESCRIPTION:
 * @AUTHOR:Administrator
 * @VERSION:v1.0
 * @DATE:2013-5-19 下午8:29:22
 *******************************************/
public class VideoNewsService {
 public static List<News> getLastNews() throws Exception {
  List<News> newes = new ArrayList<News>();
  URL url = new URL("http://169.254.161.54:8888/web/ListServlet");
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setRequestMethod("GET");
  conn.setConnectTimeout(5000);
  conn.connect();
  if (conn.getResponseCode() == 200) {
   InputStream inputStream = conn.getInputStream();
   // 因為頁面返回值是xml文件,所以現在就要解析這個xml,使用pull解析器解析
   
   newes= parseXML(inputStream);

  }
  return newes;
 }

 /**
  * @description:pull解析服務器返回xml文件
  * @author:Administrator
  * @return:List<News>
  * @param inputStream
  * @return
  * @throws XmlPullParserException
  * @throws IOException
  */

 private static List<News> parseXML(InputStream inputStream)
   throws Exception {
  XmlPullParser xmlPullParser = Xml.newPullParser();
  xmlPullParser.setInput(inputStream, "utf-8");
  int event = xmlPullParser.getEventType();
  List<News> newes = new ArrayList<News>();
  News news = null;
  while (event != xmlPullParser.END_DOCUMENT) {
   switch (event) {
   case 2:
    if ("news".equals(xmlPullParser.getName())) {
     int id = Integer
       .valueOf(xmlPullParser.getAttributeValue(0));
     news = new News();
     news.setId(id);
    } else if ("title".equals(xmlPullParser.getName())) {
     news.setTitle(xmlPullParser.nextText());
    } else if ("timelength".equals(xmlPullParser.getName())) {
     news.setTimelength(Integer.valueOf(xmlPullParser.nextText()));
    }
    break;
   case 3:
    if ("news".equals(xmlPullParser.getName())) {
     newes.add(news);
     news = null;
    }
    break;
    default:
     break;
     
   }
   
   event = xmlPullParser.next();
  }
  return newes;
 }
}

activity中使用線程調用service,從而更新UI


[html] 
package com.yehui.news; 
 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
 
import com.yehui.bean.News; 
import com.yehui.service.VideoNewsService; 
 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.app.Activity; 
import android.view.Menu; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.Toast; 
 
public class MainActivity extends Activity { 
    ListView listview; 
    private Handler handler = new Handler() { 
         
 
        @Override 
        public void handleMessage(Message msg) { 
            try { 
                List<News> newes = (List<News>) msg.obj; 
 
                List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); 
                for (News news : newes) { 
                    HashMap<String, Object> item = new HashMap<String, Object>(); 
                    item.put("title", news.getTitle()); 
                    item.put("timelength", news.getTimelength()); 
                    data.add(item); 
                } 
                SimpleAdapter simpleAdapter = new SimpleAdapter( 
                        getApplicationContext(), data, R.layout.item, 
                        new String[] { "title", "timelength" }, new int[] { 
                                R.id.title, R.id.timelength }); 
                listview.setAdapter(simpleAdapter); 
            } catch (Exception e) { 
                e.printStackTrace(); 
                Toast.makeText(getApplicationContext(), R.string.error, 1) 
                        .show(); 
            } 
        } 
 
    }; 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        listview = (ListView) this.findViewById(R.id.listview); 
        new Thread() { 
 
            @Override 
            public void run() { 
                List<News> newes; 
                try { 
                    newes = VideoNewsService.getLastNews(); 
                    Message msg = new Message(); 
                    msg.obj = newes; 
                    handler.sendMessage(msg); 
                } catch (Exception e) { 
                    e.printStackTrace(); 
                } 
 
            } 
 
        }.start(); 
 
    } 
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        getMenuInflater().inflate(R.menu.main, menu); 
        return true; 
    } 
 

package com.yehui.news;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yehui.bean.News;
import com.yehui.service.VideoNewsService;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {
 ListView listview;
 private Handler handler = new Handler() {
  

  @Override
  public void handleMessage(Message msg) {
   try {
    List<News> newes = (List<News>) msg.obj;

    List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
    for (News news : newes) {
     HashMap<String, Object> item = new HashMap<String, Object>();
     item.put("title", news.getTitle());
     item.put("timelength", news.getTimelength());
     data.add(item);
    }
    SimpleAdapter simpleAdapter = new SimpleAdapter(
      getApplicationContext(), data, R.layout.item,
      new String[] { "title", "timelength" }, new int[] {
        R.id.title, R.id.timelength });
    listview.setAdapter(simpleAdapter);
   } catch (Exception e) {
    e.printStackTrace();
    Toast.makeText(getApplicationContext(), R.string.error, 1)
      .show();
   }
  }

 };

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  listview = (ListView) this.findViewById(R.id.listview);
  new Thread() {

   @Override
   public void run() {
    List<News> newes;
    try {
     newes = VideoNewsService.getLastNews();
     Message msg = new Message();
     msg.obj = newes;
     handler.sendMessage(msg);
    } catch (Exception e) {
     e.printStackTrace();
    }

   }

  }.start();

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}

layout文件,因為顯示的時候用到了listview,所以這裡對listview的行布局文件也列出

listview布局文件:


[html]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 
 
    <ListView 
        android:id="@+id/listview" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" > 
    </ListView> 
 
</LinearLayout> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>
listview的行布局文件:


[html] 
<?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/title" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" /> 
 
    <TextView 
        android:id="@+id/timelength" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" /> 
 
</LinearLayout> 

<?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/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/timelength"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
最後將訪問網絡的權限加入到清單文件中:

<uses-permission android:name="android.permission.INTERNET"/>


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved