一丶一个简单的访问服务器(访问网易新闻客户端)
点击加载新闻:首页的布局文件
<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">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="点击显示网易新闻" />
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>
listView中的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.loopj.android.image.SmartImageView
android:id="@+id/iv"
android:layout_width="80dp"
android:layout_height="80dp" />
<TextView
android:id="@+id/tvTitle"
android:layout_toRightOf="@id/iv"
android:layout_marginLeft="5dp"
android:textSize="16dp"
android:textColor="#000000"
android:textStyle="bold"
android:text="我是标题"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tvDiscription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvTitle"
android:layout_alignLeft="@id/tvTitle"
android:textSize="12dp"
android:maxLines="3"
android:textColor="#99000000"
android:text="我是内容"
/>
<TextView
android:id="@+id/tvInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是跟帖"
android:textSize="10dp"
android:layout_marginRight="5dp"
android:layout_alignParentRight="true"
android:layout_alignBottom="@id/iv"
/>
</RelativeLayout>
2.MainActivity中代码
public void click(View view){
new Thread(){
public void run() {
//1.创建网络地址对象
String path = "http://192.168.1.184:8080/news/news.xml" ;//服务端地址
try {
URL url = new URL(path) ;
//打开连接
HttpURLConnection http = (HttpURLConnection) url.openConnection() ;
http.setConnectTimeout(5000) ;
//拿到服务器返回的状态吗
int code = http.getResponseCode() ;
//判断状态吗
if(code == 200){
//流中是xml文件
InputStream is = http.getInputStream() ;
list = StreamUtils.getNewsList(is) ;//解析XML放回的list集合
runOnUiThread(new Runnable() {
//将流中的xml文件解析成集合
@Override
public void run() {
lv.setAdapter(new MyAdapter()) ;
}
}) ;
}else{
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "请求失败", 0).show() ;
}
}) ;
}
} catch (Exception e) {
e.printStackTrace();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "网络不通,请检查网络是否开启", 0).show() ;
}
}) ;
}
};
}.start() ;
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//拿到布局填充器
LayoutInflater inflater = LayoutInflater.from(MainActivity.this) ;
//加载布局文件
View view = null ;
if(convertView == null){
view = inflater.inflate(R.layout.news, null) ;
}else{
view = convertView ;
}
//拿到布局文件中的各个控件
TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle) ;
TextView tvDesctiption = (TextView) view.findViewById(R.id.tvDiscription) ;
TextView tvInfo = (TextView) view.findViewById(R.id.tvInfo) ;
SmartImageView iv = (SmartImageView) view.findViewById(R.id.iv) ;
//拿到新闻对象
NewsItem news = list.get(position) ;
tvTitle.setText(news.getTitle()) ;
//设置图片
iv.setImageUrl(news.getImage()) ;
tvDesctiption.setText(StreamUtils.change(news.getDescription())) ;
if(news.getType().equals("1")){
tvInfo.setText("跟帖:" + news.getConnent()) ;
}else if(news.getType().equals("2")){
tvInfo.setText("专题") ;
tvInfo.setTextColor(getResources().getColor(android.R.color.holo_blue_bright)) ;
}else if(news.getType().equals("3")){
tvInfo.setText("视频") ;
tvInfo.setTextColor(getResources().getColor(android.R.color.holo_red_light)) ;
}
return view;
}
}
}
3.解析新闻XML的工具类
public static List<NewsItem> getNewsList(InputStream is) {
List<NewsItem> list = new ArrayList<NewsItem>();
try {
// 创建xml的解析器
XmlPullParser pull = Xml.newPullParser();
// 设置要解析的数据的编码
pull.setInput(is, "utf-8");
// 拿到事件类型
int type = pull.getEventType();
// 循环解析数据
NewsItem item = null;
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
case XmlPullParser.START_TAG:
if("item".equals(pull.getName())){
item = new NewsItem() ;
}else if("title".equals(pull.getName())){
item.setTitle(pull.nextText()) ;
}else if("description".equals(pull.getName())){
item.setDescription(pull.nextText()) ;
}else if("image".equals(pull.getName())){
item.setImage(pull.nextText()) ;
}else if("type".equals(pull.getName())){
item.setType(pull.nextText()) ;
}else if("comment".equals(pull.getName())){
item.setConnent(pull.nextText()) ;
}
break;
case XmlPullParser.END_TAG:
if("item".equals(pull.getName())){
list.add(item) ;
item = null ;
}
break;
}
//拿到下一个事件
type = pull.next() ;
}
} catch (Exception e) {
e.printStackTrace();
}
return list ;
}
4.肯定有一个新闻对象News(要创建一个bean包中有News类封装新闻消息)
5.别忘了记得添加访问internet权限
6.服务端创建的News(附件里面有)
注:要加载图片我们要用到smartimageview加载图片(smrtImageview包附件里面)
本文出自 “android笔记” 博客,请务必保留此出处http://2585211.blog.51cto.com/10044233/1669876
原文地址:http://2585211.blog.51cto.com/10044233/1669876