Android获取网络数据

本文介绍了如何在Android应用中利用Struts2框架从服务器获取视频数据并展示在手机上,包括配置服务器端、创建Action处理逻辑及返回JSON数据等关键步骤。

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

Android开发中,手机要从网络中获取数据,有多种方法。

有服务器端,如果要使用struts2,首先要对网站进行配置。

第一步,将struts2中需要的jar复制到WEB-INF目录lib下.本人用的jar包为:

commons-logging-1.0.4.jar

freemarker-2.3.8.jar

ognl-2.6.11.jar

struts2-core-2.0.14.jar

xwork-2.0.7.jar

然后,修改web.xml文件,其内容为:

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>VideoSite</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    
  </welcome-file-list>
  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
          org.apache.struts2.dispatcher.FilterDispatcher
      </filter-class>
  </filter>
  <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
 </filter-mapping>

</web-app>

第二步,开始创建Action.假设要从数据库中读取视频资讯,将显示在手机上。

(1)创建一个Video类,代码如下:

public class Video {
private int id;
private String title;
private int timelength;
//两个构造方法
public Video() {
	super();
	// TODO Auto-generated constructor stub
}
public Video(int id, String title, int timelength) {
	super();
	this.id = id;
	this.title = title;
	this.timelength = timelength;
}
get/set略


(2)假设从数据库中取得的数据如下:

public class VideoServiceImpl implements VideoService {

	@Override
	public List<Video> getLastVideo() {
		//查询数据库假设得到如下数据:根据要返回的值,创建一个LIST
		List<Video> videos=new ArrayList<Video>();
		//向LIST中添加对象数据
		videos.add(new Video(1,"喜洋洋与灰太狼",40));
		videos.add(new Video(2,"大闹天宫",46));
		videos.add(new Video(3,"大头儿子小头爸爸",30));
		videos.add(new Video(4,"美猴王",75));
		//返回视频数据处理
		return videos;
	}

}


(3)对返回的数据进行处理,创建Action

public class VideoListAction {
	VideoService service=new VideoServiceImpl();
	
	//返回使用JSON表示的视频信息
	@SuppressWarnings("unchecked")
	//public  String execute(){
		public  String list(){	
		//通Service得到最新视频列表
		List<Video> videos=service.getLastVideo();
		//字符串构造器
		StringBuilder json=new StringBuilder();
		//[{id:1,title:"XXX",timelength:45},{}]
		json.append("[");
		for(Video video:videos){
			json.append("{");
			json.append("id:").append(video.getId()).append(",");
			json.append("title:\"").append(video.getTitle()).append("\",");
			json.append("timelength:").append(video.getTimelength()).append("}");
			json.append(",\n");
	   	}
		json.deleteCharAt(json.length()-1);
		json.deleteCharAt(json.length()-1);
		json.append("]");
//返回值到下一页中
		ActionContext.getContext().getSession().put("json", json);
		return "json";

	}

}

(3)根据返回值,调用指定页面,因此要在src文件夹里创建struts.xml文件,其内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <!-- 指定struts2处于开发阶段,可以进行调试 -->
    <constant name="struts.devMode" value="true"/>
    <!-- 默认模块 -->
	<package name="exam-default" namespace="/"
		extends="struts-default">
		<global-results>
			<result name="error">/WEB-INF/page/error.jsp</result>
		</global-results>
	</package>
    <package name="p1" extends="struts-default">
        <action name="list" class="com.zhm.action.VideoListAction">
            <result name="json">/WEB-INF/page/jsonvideos.jsp</result>
        </action>
    </package>    
</struts>

(4)测试。在WEB-INF建立文件夹,并分别建立两个页面文件,其中jsonvideos.jsp 内容如下:

<%@ page language="java" contentType="text/plain; charset=UTF-8" 
pageEncoding="UTF-8"%>
${json}


在地址栏里输入http://localhost:8080/VideoSite/list!list.action

得到内容如下。其中,如果在Action里如果用execute()方法实现取数据,则使用测试地址为:

http://localhost:8080/VideoSite/list.action。使用前者,可以在Action里创建多个方法。

[{id:1,title:"喜洋洋与灰太狼",timelength:40},
{id:2,title:"大闹天宫",timelength:46},
{id:3,title:"大头儿子小头爸爸",timelength:30},
{id:4,title:"美猴王",timelength:75}]

这样,服务器端通过struts2 获取到了数据。然后要将数据返回到手机端。

未完待续……


  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值