需要网络权限<uses-permission android:name="android.permission.INTERNET" />
package com.example.net;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Xml;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.loopj.android.image.SmartImageView;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class NewsClient extends Activity {
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
ListView list= (ListView) findViewById(R.id.list);
list.setAdapter(new MyAdapter());
}
};
private List<News> newsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_client);
parseXml();
}
private void parseXml(){
new Thread(){
@Override
public void run() {
String path="http://192.168.0.101/news/news.xml";
/*
<?xml version="1.0" encoding="utf-8"?>
<news>
<new>
<title>标题标题</title>
<detail>详细内容</detail>
<image>http://192.168.0.101/news/images/image1.jpg</image>
<comment>10001</comment>
</new>
<new>
<title>标题标题</title>
<detail>详细内容。</detail>
<image>http://192.168.0.101/news/images/image1.jpg</image>
<comment>10001</comment>
</new>
</news>
*/
try {
URL url=new URL(path);
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
if(conn.getResponseCode()==200){
InputStream is=conn.getInputStream();
XmlPullParser xml = Xml.newPullParser();
xml.setInput(is,"utf-8");
int type=xml.getEventType();
News news=null;
while(type!=XmlPullParser.END_DOCUMENT){
switch (type){
case XmlPullParser.START_TAG:
if("news".equals(xml.getName())){
newsList=new ArrayList<News>();
}else if("new".equals(xml.getName())){
news=new News();
}else if("title".equals(xml.getName())){
news.setTitle(xml.nextText());
}else if("detail".equals(xml.getName())){
news.setDetail(xml.nextText());
}
else if("image".equals(xml.getName())){
news.setImageurl(xml.nextText());
}
else if("comment".equals(xml.getName())){
news.setComment(xml.nextText());
}
break;
case XmlPullParser.END_TAG:
if("new".equals(xml.getName())){
newsList.add(news);
}
break;
}
type=xml.next();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(0);
}
}.start();
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return newsList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v=null;
if(convertView==null){
v=View.inflate(NewsClient.this,R.layout.list,null);
}else{
v=convertView;
}
//SmartImageView为使用了别人的类库github中搜索smart-image-view复制源文件到项目
SmartImageView imageView= (SmartImageView) v.findViewById(R.id.iv_photo);
TextView tv_title= (TextView) v.findViewById(R.id.tv_title);
TextView tv_detail= (TextView) v.findViewById(R.id.tv_detail);
TextView tv_comment= (TextView) v.findViewById(R.id.tv_comment);
News news=newsList.get(position);
//直接将图片下载添加上去
imageView.setImageUrl(news.getImageurl());
tv_title.setText(news.getTitle());
tv_detail.setText(news.getDetail());
tv_comment.setText(news.getComment()+"条评论");
return v;
}
}
}
布局
<RelativeLayout 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"
tools:context="com.example.net.NewsClient">
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.loopj.android.image.SmartImageView
android:layout_centerVertical="true"
android:id="@+id/iv_photo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:src="@drawable/image1" />
<TextView
android:layout_marginTop="10dp"
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/iv_photo"
android:singleLine="true"
android:textSize="16sp"
android:text="山东平邑县委书记县长被免职" />
<TextView
android:id="@+id/tv_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/iv_photo"
android:lines="2"
android:layout_below="@id/tv_title"
android:textSize="16sp"
android:textColor="#55000000"
android:text="连续发生生产安全事故,常务副县长和分管副县长也被免职" />
<TextView
android:id="@+id/tv_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_detail"
android:layout_alignParentRight="true"
android:textColor="#88ff0000"
android:text="10001条评论" />
</RelativeLayout>
news类
package com.example.net;
/**
* Created by joy on 2015/12/29.
*/
public class News {
private String title;
private String detail;
private String imageurl;
private String comment;
public News(String title, String detail, String imageurl, String comment) {
this.title = title;
this.detail = detail;
this.imageurl = imageurl;
this.comment = comment;
}
public News() {
}
public String getTitle() {
return title;
}
public String getDetail() {
return detail;
}
public String getImageurl() {
return imageurl;
}
public String getComment() {
return comment;
}
public void setTitle(String title) {
this.title = title;
}
public void setDetail(String detail) {
this.detail = detail;
}
public void setImageurl(String imageurl) {
this.imageurl = imageurl;
}
public void setComment(String comment) {
this.comment = comment;
}
}