首先说一下我用的是Android4.2.2(Level 17),在AndroidManifest.xml文件中默认的配置是:
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
但是这样连不上网,改成:
<uses-sdk android:minSdkVersion="8" />
就可以联网了。可能是SdkVersion 17的问题还是其他,我也不知道。
以下是练习代码:
当然在AndroidManifest.xml要加上联网权限
<uses-permission android:name="android.permission.INTERNET" />
activity_main.xml
<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/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
列表项样式news_item.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="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/title"
android:layout_width="200dp"
android:layout_height="match_parent"
android:text="adsfs"/>
<TextView
android:id="@+id/time"
android:layout_width="fill_parent"
android:text="adsfs"
android:layout_height="match_parent" />
</LinearLayout>
News.java
package com.lipeng.model;
public class News {
private Integer id;
private String title;
private int time;
public News() {
}
public News(Integer id, String title, int time) {
this.id = id;
this.title = title;
this.time = time;
}
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 int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
@Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", time=" + time + "]";
}
}
NewsService.java
package com.lipeng.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 com.lipeng.model.News;
public class NewsService {
/**
* 获取最新资讯
* */
public List<News> getLastNews() throws Exception{
String path = "http://192.168.241.1:8080/test//servlet/NewsServlet";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
List<News> listNews = null;
if(conn.getResponseCode() == 200){
InputStream is = conn.getInputStream();
listNews = newsPullXMLParser(is);
}
return listNews;
}
/**
* 最新资讯的Pull方式获取的XML解析器
* @param is 从url中获取的输入流
* @return
* @throws Exception
*/
public List<News> newsPullXMLParser(InputStream is) throws Exception {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(is, "UTF-8");
int eventType = parser.getEventType();
List<News> newses = new ArrayList<News>();
News news = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
String nodeName = parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if("news".equals(nodeName)){
news = new News();
news.setId(Integer.parseInt(parser.getAttributeValue(0)));
} else if("title".equals(nodeName)){
news.setTitle(parser.nextText().toString());
} else if("timelength".equals(nodeName)){
news.setTime(Integer.parseInt(parser.nextText()));
}
break;
case XmlPullParser.END_TAG:
if("news".equals(nodeName)){
newses.add(news);
news = null;
}
break;
}
eventType = parser.next();
}
return newses;
}
}
MainActivity.java
package com.lipeng.netnews;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.lipeng.model.News;
import com.lipeng.service.NewsService;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
private ListView listView = null;
NewsService service = new NewsService();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
List<News> listNews = null;
try {
listNews = service.getLastNews();
} catch (Exception e) {
e.printStackTrace();
}
for(News news : listNews){
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", news.getId());
map.put("title", news.getTitle());
map.put("time", news.getTime());
data.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, data, R.layout.news_item, new String[]{"title", "time"}, new int[]{R.id.title, R.id.time});
listView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
在J2EE的WEB端是:
目的请求一个xml文件:
<?xml version="1.0" encoding="UTF-8"?>
-<videonews>
-<news id="1">
<title>青年医生</title>
<timelength>10</timelength>
</news>
+<news id="2">
-<news id="3">
<title>星球大战</title>
<timelength>112</timelength>
</news>
</videonews>
videonews.jsp
要说明的是第一行不能格式化,如果格式化就在浏览器上显示不出来,但是源码文件依然是对的。
<%@ page language="java" contentType="text/xml; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><?xml version="1.0" encoding="UTF-8"?>
<videonews><c:forEach items="${newses}" var="video">
<news id="${video.id}">
<title>${video.title}</title>
<timelength>${video.time}</timelength>
</news></c:forEach>
</videonews>
后面就是写一个Servlet然后servlet中请求关键代码是:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<News> newses = service.findAllNews();
request.setAttribute("newses", newses);
request.getRequestDispatcher("/WEB-INF/pages/videonews.jsp").forward(request, response);
}