1.newsClient新闻客户端涉及知识点汇总:
(1)ListView(用来显示消息)
(2)开子线程去服务器取数据
(3)解析xml文件
(4)利用handler或者runOnUiThread(new Runnable(){})更新ui组件。
(5)设置ListView的数据适配器MyAdapter(继承BaseAdapter)
(6)开源项目SmartImageView的使用
(7)tomcat服务器的搭建(此项目在运行之前一定会开启服务器)
(8)在Manifest文件中添加网络访问权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
2.项目源码
<1>xml布局文件
(1)MainActivity.class的布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ListView android:id="@+id/lv_news" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </LinearLayout>
(2)ListView组件的item布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="80dp"> <com.loopj.android.image.SmartImageView android:id="@+id/iv_icon" android:layout_width="80dp" android:layout_height="80dp" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="true" android:ellipsize="end" android:layout_toRightOf="@+id/iv_icon" android:textSize="22dp" android:textColor="#000000" android:text="TextView" /> <TextView android:id="@+id/tv_detail" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="2" android:ellipsize="end" android:layout_toRightOf="@+id/iv_icon" android:layout_below="@+id/tv_title" android:textSize="18dp" android:textColor="#000000" android:text="TextView11111111111111111111111111111111111111111111111" /> <TextView android:id="@+id/tv_type" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:textSize="14sp" android:layout_alignBottom="@+id/iv_icon" android:text="TextView" /> </RelativeLayout>
注意:
ListView的item采用相对布局
android:singleLine="true" //这句设置textview组件只显示单行
android:maxLines="2" //这句设置textview组件最多显示的行数
android:layout_toRightOf="@+id/iv_icon" //设置该组件在iv_icon组件的右边
android:layout_below="@+id/tv_title" //设置该组件在tv_title组件的下边
android:layout_alignParentRight="true" --将控件的右边缘和父控件的右边缘对齐
<2>java后台代码
(1)MainActivity.java
package com.example.lucky.test55newsclient1; 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.loopj.android.image.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 { ListView lv_news; List<News> newsLists; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv_news=findViewById(R.id.lv_news); //准备lv_news需要显示的数据,从服务器中获取数据,进行封装 initListData(); } //准备lv_news需要显示的数据 private void initListData() { new Thread(){ @Override public void run() { try { //0.去服务器取数据 http://192.168.1.142:8080/news.xml //1.创建URL,指定我们要访问的网址 String path="http://192.168.1.142:8080/news.xml"; URL url=new URL(path); //2.获取HttpURLConnection对象,用于发送或接收数据 HttpURLConnection conn= (HttpURLConnection) url.openConnection(); System.out.println("-------------------flaglucky"); //3.设置请求格式为get conn.setRequestMethod("GET"); //注意get要求大写,默认为get请求 //4.设置请求的超时时间 conn.setConnectTimeout(5000); //5.获取服务器返回的状态码 int code=conn.getResponseCode(); System.out.println("--------------"+code); //如果code==200说明请求成功 if(code==200) { System.out.println("-------------------flagif"); //6.获取服务器返回的数据(以流的形式返回) InputStream in = conn.getInputStream(); //7.解析xml newsLists= XmlParserUtils.parserXml(in); runOnUiThread(new Runnable() { @Override public void run() { //更新ui,将数据显示到listview中 lv_news.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 i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(int i, View view, ViewGroup viewGroup) { View viewshow; if(view==null){ viewshow=View.inflate(getApplicationContext(),R.layout.items,null); }else { viewshow=view; } //1.找到控件 SmartImageView iv_icon=viewshow.findViewById(R.id.iv_icon); TextView tv_title=viewshow.findViewById(R.id.tv_title); TextView tv_detail=viewshow.findViewById(R.id.tv_detail); //2.显示数据 tv_title.setText(newsLists.get(i).getTitle()); tv_detail.setText(newsLists.get(i).getDetail()); String imageUrl=newsLists.get(i).getImage(); iv_icon.setImageUrl(imageUrl); return viewshow; } } }
(2)javabean(News.java)
package com.example.lucky.test55newsclient1; public class News { private String title; private String detail; private String comment; private String image; 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 String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } }
(3)XmlParserUtils工具类
package com.example.lucky.test55newsclient1; 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; public class XmlParserUtils { 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){ switch (type){ case XmlPullParser.START_TAG: //解析开始节点 //5.判断正在解析的是哪个标签 if("newslist".equals(parser.getName())){ //6.创建一个list集合 newslists=new ArrayList<>(); }else if("news".equals(parser.getName())){ news=new News(); }else if("title".equals(parser.getName())){ news.setTitle(parser.nextText()); }else if("detail".equals(parser.getName())){ news.setDetail(parser.nextText()); }else if("comment".equals(parser.getName())){ news.setComment(parser.nextText()); }else if("image".equals(parser.getName())){ news.setImage(parser.nextText()); } break; case XmlPullParser.END_TAG: //解析结束节点 if("news".equals(parser.getName())){ //把javabean添加到集合 newslists.add(news); } System.out.println("-----------------2019/3/31"); break; } //不停的向下解析 type=parser.next(); } return newslists; } }
(4)SmartImageView的使用
将开源项目解压放入项目中
调用即可。
3.项目效果图