package com.example.a33_news_client;
import android.content.SyncStatusObserver;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import loopj.android.image.SmartImageView;
public class MainActivity extends AppCompatActivity {
private ListView lv;
private List<News> newsLists;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1 找到我们关心的控件
lv = (ListView) findViewById(R.id.lv);
//2 准备listview要显示的数据 去服务器取数据 进行封装
initListData();
}
private void initListData() {
new Thread() {
public void run() {
try {
//2 去服务器取数据 http://192.168.11.86:8080/news.xml
String path = "http://192.168.11.86:8080/news.xml";
//2.2 创建URL 对象指定我们要访问的网址(路径)
URL url = new URL(path);
//2.3 拿到httpurlconnection对象 用于发送或者接收数据
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//2.4 设置发送get请求
conn.setRequestMethod("GET");
//2.5 设置请求超时时间
conn.setConnectTimeout(5000);
//2.6 获取服务器返回的状态码
int code = conn.getResponseCode();
//2.7 如果code == 200 说明请求成功
if (code == 200) {
//2.8 获取服务器返回的数据 是以流的形式返回的
InputStream in = conn.getInputStream();
//2.9 解析xml 抽出一个业务方法
newsLists = XmlParserUtils.parserXml(in);
System.out.println("newsLists:"+newsLists.size());
runOnUiThread(new Runnable() {
@Override
public void run() {
//3 更新UI 把数据显示到listview上
lv.setAdapter(new MyAdapter());
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return newsLists.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;
if (convertView == null) {
view = View.inflate(getApplicationContext(), R.layout.item, null);
} else {
view = convertView;
}
//1 找到控件 显示集合里面的数据
SmartImageView iv_icon = (SmartImageView) view.findViewById(R.id.iv_icon);
TextView tv_title = (TextView) findViewById(R.id.tv_title);
TextView tv_desc = (TextView) findViewById(R.id.tv_desc);
TextView tv_type = (TextView) findViewById(R.id.tv_type);
//1.1 显示图片的数据
String imageUrl = newsLists.get(position).getImage();
//展示图片 得到的资源图片,备用资源
iv_icon.setImageUrl(imageUrl, R.mipmap.ic_launcher);
//2 显示数据
tv_title.setText(newsLists.get(position).getTitle());
tv_desc.setText(newsLists.get(position).getDescription());
String typee = newsLists.get(position).getType();
String comment = newsLists.get(position).getComment();
int type = Integer.parseInt(typee);
switch (type) {
case 1:
tv_type.setText(comment+"国内");
break;
case 2:
tv_type.setText("跟帖");
break;
case 3:
tv_type.setText("国外");
break;
default:
break;
}
return view;
}
}
}
package com.example.a33_news_client;
import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2017/4/19.
*/
public class XmlParserUtils {
//解析xml的业务方法
public static List<News> parserXml(InputStream in) throws Exception {
List<News> newsLists = null;
News news = null;
//1 获取xml的解析器
XmlPullParser parser = Xml.newPullParser();
//2 设置解析器要解析的内容
parser.setInput(in, "utf-8");
//3 获取解析器的事件类型
int type = parser.getEventType();
//4 不停的向下解析
while (type != XmlPullParser.END_DOCUMENT) {
//5 具体判断一下解析的是开始节点 还是结束节点
switch (type) {
case XmlPullParser.START_TAG: //解析开始节点
//6 具体判断一下解析的是哪个开始标签
if ("channel".equals(parser.getName())) {
//创建一个List集合
newsLists = new ArrayList<News>();
} else if ("item".equals(parser.getName())) {
news = new News();
} else if ("title".equals(parser.getName())) {
news.setTitle(parser.nextText());
} else if("description".equals(parser.getName())) {
news.setDescription(parser.nextText());
} else if("image".equals(parser.getName())) {
news.setImage(parser.nextText());
} else if("type".equals(parser.getName())) {
news.setType(parser.nextText());
} else if ("comment".equals(parser.getName())) {
news.setComment(parser.getText());
}
break;
case XmlPullParser.END_TAG: //解析结束标签
if("item".equals(parser.getName())) {
//把javabean添加到集合
newsLists.add(news);
}
break;
}
//不停的向下解析
type = parser.next();
}
return newsLists;
}
}
news.xml这个获取地址挂掉了,所以无法演示,代码还是靠谱的。
另外用到了github开源项目的SmartImageView里的控件,直接解析url就可以直接显示图片,不用自己搞图片查看器来解析,很方便的接口。
接口获取方式:直接去github搜索smartimageview 找点赞数最多的那个就可以了。