源码下载网站:https://github.com/
运用下载的api SmartImage
新闻客户端
介绍
1.UI效果是公司里面美工去画的
2.应用的传输数据,定义借接口
3.关于xml的数据是服务器开发人员通过一定的技术手段返回的,对于Android开发人员我们要做的就是解析,把我们关心的数据取出来,展示到Android中控件上
实现
1.MainActivity
package activitytest.example.com.news;
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.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.jit.lib.SmartImage;
import com.jit.lib.SmartImageView;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List newsList;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//【1】找到控件
lv = findViewById(R.id.lv);
//【2】准备ListView数据要显示的数据,去服务器取数据进行封装
initListData();
}
//准备ListView的数据
private void initListData() {
new Thread() {
@Override
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 抽出一个业务方法
newsList = XmlParserUtils.parserXml(in); //解析完,要展示的数据放到了集合中
//【3】更新ui 把数据展示到listview上
runOnUiThread(new Runnable() {
@Override
public void run() {
lv.setAdapter(new MyAdapter());
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
//定义数据适配器
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return newsList.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v;
if (view == null) {
v = View.inflate(MainActivity.this, R.layout.item, null);
} else {
v = view;
}
//【1】找到控件显示集合里面的数据
SmartImageView iv_icon = v.findViewById(R.id.iv_icon);
TextView tv_title = v.findViewById(R.id.tv_title);
TextView tv_desc = v.findViewById(R.id.tv_desc);
TextView tv_type = v.findViewById(R.id.tv_type);
//【1.1】展示图片的数据
String imageUrl = newsList.get(i).getImage();
//下载SmartImage
iv_icon.setImageUrl(imageUrl);
//【2】显示数据
tv_title.setText(newsList.get(i).getTitle());
tv_desc.setText(newsList.get(i).getDescription());
String typee = newsList.get(i).getType();
String comment = newsList.get(i).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;
}
return v;
}
}
}
2.News
package activitytest.example.com.news;
public class News {
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
private String title;
private String description;
private String image;
private String type;
private String comment;
}
3.XmlParserUtils
package activitytest.example.com.news;
import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class XmlParserUtils {
//解析xml的业务方法
public static List parserXml(InputStream in) throws Exception{
List 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())){ //如果是channel标签
//创建一个list集合
newsLists = new ArrayList();
}else if ("item".equals(parser.getName())){
news = new News();
}else if ("tetle".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.nextText());
}
break;
case XmlPullParser.END_TAG: //解析结束的标签
if ("item".equals(parser.getName())){
newsLists.add(news); //将javabean对象加到集合中
}
break;
}
//不停的向下解析
type = parser.next();
}
return newsLists;
}
}
4.activity_main.xml
5.item.xml