package cn.itcast.service;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import android.util.Xml;
import cn.itcast.domain.News;
import cn.itcast.utils.StreamTool;
public class VideoNewsService {
/**
* 获取最新的视频资讯
* @return
* @throws Exception
*/
public static List<News> getJSONLastNews() throws Exception{
String path = "http://192.168.0.168:8080/web/ListServlet?format=json";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode() == 200){
InputStream inStream = conn.getInputStream();
return parseJSON(inStream);
}
return null;
}
/**
* 解析JSON数据
* @param inStream
* @return
*/
private static List<News> parseJSON(InputStream inStream) throws Exception{
List<News> newses = new ArrayList<News>();
byte[] data = StreamTool.read(inStream);
String json = new String(data);
JSONArray array = new JSONArray(json);
for(int i = 0 ; i < array.length() ; i++){
JSONObject jsonObject = array.getJSONObject(i);
News news = new News(jsonObject.getInt("id"), jsonObject.getString("title"), jsonObject.getInt("timelength"));
newses.add(news);
}
return newses;
}
/**
* 获取最新的视频资讯
* @return
* @throws Exception
*/
public static List<News> getLastNews() throws Exception{
String path = "http://192.168.0.168:8080/web/ListServlet";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode() == 200){
InputStream inStream = conn.getInputStream();
return parseXML(inStream);
}
return null;
}
/**
* 解析服务器返回的xml数据
<?xml version="1.0" encoding="UTF-8" ?>
<videonews>
<news id="35">
<title>喜羊羊与灰太狼全集</title>
<timelength>90</timelength>
</news>
<news id="12">
<title>老张与灰太狼</title>
<timelength>20</timelength>
</news>
<news id="56">
<title>老方与LILI</title>
<timelength>30</timelength>
</news>
</videonews>
* @param inStream
* @return
*/
private static List<News> parseXML(InputStream inStream) throws Exception {
List<News> newses = new ArrayList<News>();
News news = null;
XmlPullParser parser = Xml.newPullParser();
parser.setInput(inStream, "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("title".equals(parser.getName())){
news.setTitle(parser.nextText());
}else if("timelength".equals(parser.getName())){
news.setTimelength(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;
}
}
把数据绑定到LIST上
package cn.itcast.news;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import cn.itcast.domain.News;
import cn.itcast.service.VideoNewsService;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView) this.findViewById(R.id.listView);
try {
List<News> videos = VideoNewsService.getLastNews();//需修改成你本机的Http请求路径
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", getResources().getString(R.string.timelength)
+ news.getTimelength()+ getResources().getString(R.string.min));
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();
}
}
<!-- 访问internet权限 -->
<uses-permission android:name="android.permission.INTERNET"/>
分享到:
相关推荐
JavaEE源代码 xml-apisJavaEE源代码 xml-apisJavaEE源代码 xml-apisJavaEE源代码 xml-apisJavaEE源代码 xml-apisJavaEE源代码 xml-apisJavaEE源代码 xml-apisJavaEE源代码 xml-apisJavaEE源代码 xml-apisJavaEE源...
JavaEE
里面包含了Json所需要的所有jar包,commons-beanutils-1.9.3.jar 、commons-codec-1.10.jar、commons-collections4-4.1.jar、commons-httpclient-3.0.1.jar、commons-httpclient-3.1.jar、commons-lang3-3.5.jar、...
JavaEE 程序设计与应用开发 源代码 郭克华主编
JavaEE源代码 JSPJavaEE源代码 JSPJavaEE源代码 JSPJavaEE源代码 JSPJavaEE源代码 JSPJavaEE源代码 JSPJavaEE源代码 JSPJavaEE源代码 JSPJavaEE源代码 JSPJavaEE源代码 JSPJavaEE源代码 JSPJavaEE源代码 JSPJavaEE源...
javaEE android 简单界面对比
本代码包括客户端和服务端两部分,客户端实现用户的注册和登录,并发送信息至服务端;服务端通过MySQL数据库对接收到的注册信息进行增删改等,实现一个完整的交互过程。
JavaEE应用开发教程代码.rar
本文针对一个基于Android+JAVAEE架构的校园信息交互系统的设计与开 发工作进行论述与说明。首先,简单的介绍校园信息交互系统相关的研究背景, 包括涉及的开发平台及技术规范;其次,分析说明了整个系统的设计与架构...
该代码是javaEE随书代码,来自清华大学出版社的javaEE轻量级框架应用与开发
javaee网上书店源代码
Javaee第6章源代码
JavaEE开发技术与案例教程第2版-课件和代码.rar
JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0...
JavaEE源代码 hibernate3JavaEE源代码 hibernate3JavaEE源代码 hibernate3JavaEE源代码 hibernate3JavaEE源代码 hibernate3JavaEE源代码 hibernate3JavaEE源代码 hibernate3JavaEE源代码 hibernate3JavaEE源代码 ...
JavaEE源代码 spring-coreJavaEE源代码 spring-coreJavaEE源代码 spring-coreJavaEE源代码 spring-coreJavaEE源代码 spring-coreJavaEE源代码 spring-coreJavaEE源代码 spring-coreJavaEE源代码 spring-coreJavaEE源...
<<JavaEE程序设计与应用开发>>源代码
JavaEE软件开发模式
《JavaEE 5开发基础与实践》第3章课件及代码 第3章_Javascript开发.rar
求精要诀——JavaEE编程开发案例精讲 文件过大无法全部上传该部分是 1-7章实践部分 源代码