package com.xh.tx.news;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import com.loopj.android.image.SmartImageView;
import com.xh.tx.entity.News;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.Xml;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
protected static final int FAILED = 0;
protected static final int SUCCESS = 1;
private final String TAG = "MainActivity";
List<News> list;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what == SUCCESS)
{
list = (List<News>) msg.obj;
ListView listview = (ListView) findViewById(R.id.lv_content);
listview.setAdapter(new ShowNewsAdapter());
}else
{
Toast.makeText(MainActivity.this, "请求网络数据失败", 0).show();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
//完成从网络上下载数据,并解析成list,供ShowNewsAdapter显示调用
private void init()
{
new Thread(new Runnable() {
@Override
public void run() {
//用HttpClient 的get方式去请求数据
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://10.0.2.2:8080/NetEaseServer/new.xml");
try {
HttpResponse response = client.execute(get);
int statuscode = response.getStatusLine().getStatusCode();
if(200 == statuscode)
{
//如果状态等于200,解析传递回来的流
InputStream in = response.getEntity().getContent();
list = getNewsFromInputStream(in);
Message message = new Message();
if(null == list)
{
message.what = FAILED;
}else{
message.what = SUCCESS;
message.obj = list;
}
handler.sendMessage(message);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
//解析xml
private List<News> getNewsFromInputStream(InputStream in)
{
XmlPullParser parser = Xml.newPullParser();
List<News> list = null;
try {
parser.setInput(in, "utf-8");
int eventType = parser.getEventType();
String name = null;
News news = null;
while(eventType != parser.END_DOCUMENT)
{
name = parser.getName();
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
list = new ArrayList<News>();
break;
case XmlPullParser.START_TAG:
if("new".equals(name))
{
news = new News();
}else if("title".equals(name))
{
news.setTitle(parser.nextText());
}else if("detail".equals(name))
{
news.setDetail(parser.nextText());
}else if("comment".equals(name))
{
news.setComment(Integer.parseInt(parser.nextText()));
}else if("image".equals(name))
{
news.setImageurl(parser.nextText());
}
break;
case XmlPullParser.END_TAG:
if("new".equals(name))
{
list.add(news);
}
break;
default:
break;
}
eventType = parser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
class ShowNewsAdapter extends BaseAdapter
{
@Override
public int getCount() {
return list.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 view = null;
if(convertView != null)
{
view = convertView;
}else
{
LayoutInflater flater = MainActivity.this.getLayoutInflater();
view = flater.inflate(R.layout.list_item, null);
}
ImageView image = (ImageView) view.findViewById(R.id.img);
TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
TextView tv_common = (TextView) view.findViewById(R.id.tv_common);
News news = list.get(position);
Log.i(TAG, news.getImageurl() + "");
image.setImageURI(Uri.parse(news.getImageurl()));
tv_title.setText(news.getTitle());
tv_content.setText(news.getDetail());
tv_common.setText(news.getComment() + "");
return view;
}
}
}
//News
package com.xh.tx.entity;
public class News
{
private int id;
private String title;
private String detail;
private Integer comment;
private String imageurl;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public Integer getComment() {
return comment;
}
public void setComment(Integer comment) {
this.comment = comment;
}
public String getImageurl() {
return imageurl;
}
public void setImageurl(String imageurl) {
this.imageurl = imageurl;
}
@Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", detail=" + detail
+ ", comment=" + comment + ", imageurl=" + imageurl + "]";
}
public News() {
super();
}
public News(int id, String title, String detail, Integer comment,
String imageurl) {
super();
this.id = id;
this.title = title;
this.detail = detail;
this.comment = comment;
this.imageurl = imageurl;
}
public News(String title, String detail, Integer comment, String imageurl) {
super();
this.title = title;
this.detail = detail;
this.comment = comment;
this.imageurl = imageurl;
}
}