网络通信之咨询客户端

案例C:酷6网的视频客户端有一个功能:“在手机上显示最新的视频咨询”,视频咨询是从服务器获取的,数据以xml格式返回给Android客户端,然后列表显示在手机上。


原理:





步骤:


——、web端的编写:


1.VideoNewService.java


package cn.edu.hpu.videonews.interFace;


import java.util.List;


import cn.edu.hpu.videonews.model.News;


public interface VideoNewService {


public List<News> getLastNews();


}


2.News.java


package cn.edu.hpu.videonews.model;


public class News {
private int id;
private String name;
private int timelength;

public News(){}

public News(int id, String name, int i) {
super();
this.id = id;
this.name = name;
this.timelength = i;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTimelength() {
return timelength;
}
public void setTimelength(int timelength) {
this.timelength = timelength;
}
}



3.VideoNewServiceImpl.java


package cn.edu.hpu.videonews.service;


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


import cn.edu.hpu.videonews.interFace.VideoNewService;
import cn.edu.hpu.videonews.model.News;


public class VideoNewServiceImpl implements VideoNewService {
public List<News> getLastNews() {
List<News> news = new ArrayList<News>();
news.add(new News(35,"喜羊羊与灰太狼",90));
news.add(new News(35,"喜羊羊与灰太狼",90));
news.add(new News(35,"喜羊羊与灰太狼",90));
return news;
}
}



4.getLastNewsServlet.java


package cn.edu.hpu.videonews.servlet;


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


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


import cn.edu.hpu.videonews.interFace.VideoNewService;
import cn.edu.hpu.videonews.model.News;
import cn.edu.hpu.videonews.service.VideoNewServiceImpl;


public class getLastNewsServlet extends HttpServlet {
private VideoNewService service = new VideoNewServiceImpl();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


doPost(request, response);
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<News> videos = service.getLastNews();
request.setAttribute("videos",videos ); 
request.getRequestDispatcher("/WEB-INF/page/videonews.jsp").forward(request, response);
}


}



在服务器上部署工程之后访问getLastNewsServlet显示如图结果:




二、Android端:



1.MainActivity.java

public class MainActivity extends Activity {
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

lv = (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("name", news.getName());
item.put("timelength", news.getTimelength());
data.add(item);

}
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.items,
new String[] {"name","timelength"}, new int[]{R.id.name,R.id.timelength});

lv.setAdapter(adapter);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}


}



2.News.java和web端一样。


3.VideoNewsService.java


public class VideoNewsService {
public static List<News> getLastNews() throws Exception{
String path = "http://192.168.0.87:8080/videonews/getLastNewsServlet";
Log.i("yang","7777777777777777777777777777777777"+ path);
URL url = new URL(path);

HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");

if(conn.getResponseCode()==200) {

InputStream instream = conn.getInputStream();
Log.i("yang", "99999999999999"+instream.toString());
return parseXML(instream);
}


return null;

}


private static List<News> parseXML(InputStream instream) throws Exception{

// TODO Auto-generated method stub
List<News> nes = 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.setName(parser.nextText());

}else if("timelength".equals(parser.getName())) {
news.setTimelength(new Integer(parser.nextText()));
}
break;
case XmlPullParser.END_TAG:
if("news".equals(parser.getName())) {
nes.add(news);
Log.i("yang", "44444444"+news.toString());
news=null;
}break;
}
event = parser.next();


}


Log.i("yang", "44444444"+nes.toString());
return nes;

}
}


4.main.xml


<?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="vertical" >
   
     <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:id="@+id/listView"/>
    
    


</LinearLayout>


5.items.xml


<?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="vertical" >
    
    <LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView android:layout_width="200dp"
android:layout_height="50dp" 
android:id="@+id/name"
android:text="名称" />
<TextView android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="@+id/timelength"
android:text="时长" />
</LinearLayout>
    
    
    


</LinearLayout>



最后的也是最重要的,因为要访问网络,所以要在AndroidManifest.xml中提供访问网络的权限。

添加代码:


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


最后运行:得到如图所示结果:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值