新闻客户端
来源:互联网 发布:javascript array 编辑:程序博客网 时间:2024/05/17 02:17
一.创建一个名为“新闻客户端”的应用程序,相应的布局文件如下(activity_main.xml)
如图所示:
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.administrator.news.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/loading"
android:visibility="invisible"
android:gravity="center"
android:orientation="vertical"
>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在加载。。。" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv_news">
</ListView>
</FrameLayout>
</LinearLayout>
二.创建ListView Item的布局
如图所示:
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="65dp">
<com.loopj.android.image.SmartImageView
android:layout_width="80dp"
android:layout_height="60dp"
android:id="@+id/siv_con"
android:layout_alignParentLeft="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/siv_con"
android:text="我是标题"
android:maxLength="20"
android:singleLine="true"
android:ellipsize="end"
android:textColor="#000000"
android:textSize="18sp"
/>
<TextView
android:id="@+id/tv_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_title"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/siv_con"
android:text="我是描述"
android:maxLength="16"
android:singleLine="true"
android:ellipsize="end"
android:textColor="#99000000"
android:textSize="14sp"
/>
<TextView
android:id="@+id/tv_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:text="评论"
android:textColor="#99000000"
android:textSize="12sp"
/>
</RelativeLayout>
三.创建NewsInfo类
在适配ListView的eitem布局时用到此对象
代码如下:
package cn.edu.bzu.anew;
/**
* Created by lenovo on 2017/5/23.
*/
public class NewsInfo {
private String iconPath;
private String title;
private String description;
private int type;
private long comment;
public String getIconPath() {
return iconPath;
}
public void setIconPath(String iconPath) {
this.iconPath = iconPath;
}
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 int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public long getComment() {
return comment;
}
public void setComment(long comment) {
this.comment = comment;
}
}
四.创建工具类解析xml文件
由于从服务器上获取的是一个XML文件,因此需要使用XmlPulParser对象解析出xml文件中的内容并设置到相应的javabean中。将解析操作的逻辑放在工具类NewsInfoService中,代码如下:
package cn.edu.bzu.anew.cn.edu.bzu.XML;
import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import cn.edu.bzu.anew.NewsInfo;
public class NewsInfoService {
//解析服务器返回的xml信息,获取所有新闻数据实体
public static List<NewsInfo> getNewsInfos(InputStream is){
//获取XmlPullParser对象
XmlPullParser parser= Xml.newPullParser();
try{
parser.setInput(is,"utf_8");
//获取指针
int type=parser.getEventType();
List<NewsInfo> newsInfos=null;
NewsInfo newsInfo=null;
//type不是文档结束
while(type!=XmlPullParser.END_DOCUMENT){
switch (type){
case XmlPullParser.START_TAG:
//拿到标签名并判断
if("news".equals(parser.getName())){
newsInfos=new ArrayList<NewsInfo>();
}else if("newsInfo".equals(parser.getName())){
newsInfo=new NewsInfo();
}else if("icon".equals(parser.getName())){
//获取解析器当前指向元素的下一个文本节点的值
String icon=parser.nextText();
newsInfo.setIconPath(icon);
}else if("title".equals(parser.getName())){
String title=parser.nextText();
newsInfo.setTitle(title);
}else if("content".equals(parser.getName())){
String description=parser.nextText();
newsInfo.setDescroption(description);
}else if("type".equals(parser.getName())){
String newsType=parser.nextText();
newsInfo.setType(Integer.parseInt(newsType));
}else if("comment".equals(parser.getName())){
String comment=parser.nextText();
newsInfo.setComment(Long.parseLong(comment));
}
break;
case XmlPullParser.END_TAG:
if("newsInfo".equals(parser.getName())){
newsInfos.add(newsInfo);
newsInfo=null;
}
break;
}
type=parser.next();
}
return newsInfos;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
}
五.编写界面交互代码
用于实现获取服务器的NewsInfo.xml文件解析并将解析的信息设置到ListView显示在界面上。下载jar包,将jar文件粘贴在工程目录下的libs文件夹下,然后右击选择add the library即可,急需要以下jar包,可以不去网站自己下载
android-async-http-1.4.8.
android-smart-image-view-1.0.0
httpcore-4.4.4
代码如下
package cn.edu.bzu.anew;
import android.graphics.Color;
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.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.image.SmartImageView;
import org.apache.http.Header;
import java.io.ByteArrayInputStream;
import java.util.List;
import cn.edu.bzu.anew.cn.edu.bzu.XML.NewsInfoService;
public class MainActivity extends AppCompatActivity {
private ListView lv_news;
private LinearLayout loading;
private List<NewsInfo> newsInfos;
//ListView 适配器
private class NewsAdapter extends BaseAdapter {
//ListView的item数
public int getCount() {
return newsInfos.size();
}
//得到ListView条目视图
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(MainActivity.this, R.layout.activity_main, null);
SmartImageView siv = (SmartImageView) view.findViewById(R.id.siv_con);
TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
TextView tv_description = (TextView) view.findViewById(R.id.tv_description);
TextView tv_type = (TextView) view.findViewById(R.id.tv_type);
NewsInfo newsInfo = newsInfos.get(position);
// SmartImageView加载指定路径图片
siv.setImageUrl(newsInfo.getIconPath(), R.drawable.ab, R.mipmap.ic_launcher);
//设置新闻标题
tv_title.setText(newsInfo.getTitle());
//设置新闻描述
tv_description.setText(newsInfo.getDescription());
int type = newsInfo.getType();//1一般新闻2.专题3.live
//不同的新闻类型设置不同的颜色和不同的内容
switch (type) {
case 1:
tv_type.setText("评论:" + newsInfo.getComment());
break;
case 2:
tv_type.setTextColor(Color.RED);
tv_type.setText("专题");
break;
case 3:
tv_type.setTextColor(Color.BLUE);
tv_type.setText("LIVE");
break;
}
return view;
}
//条目对象
public Object getItem(int position) {
return null;
}
//条目id
public long getItemId(int position) {
return 0;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_news = (ListView) findViewById(R.id.lv_news);
loading = (LinearLayout) findViewById(R.id.loading);
fillData2();
}
//使用AsyncHttpClient访问网络
private void fillData2() {
//创建AsyncHttpClient的实例
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
//使用get方式请求
asyncHttpClient.get(getString(R.string.serverurl), new AsyncHttpResponseHandler() {
public void onSuccess(String content) {
//访问成功
super.onSuccess(content);
//将字符串转换成数组
byte[] bytes = content.getBytes();
//将数组转换成输入流
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
//调用NewsInfoService工具类解析xml文件
newsInfos = NewsInfoService.getNewsInfos(bais);
if (newsInfos == null) {
//解析失败 弹出toast
Toast.makeText(MainActivity.this, "解析失败", Toast.LENGTH_LONG).show();
} else {
//更新界面
loading.setVisibility(View.INVISIBLE);
lv_news.setAdapter(new NewsAdapter());
}
}
//请求失败
public void onFailure( Throwable error,String content) {
// super.onFailure(error, content);
Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_LONG).show();
}
});
}
}
六.配置服务器
因为需要从服务器上下载一个XML,因此需要开启Tomcat服务器。在Tomcat根目录下找到bin文件夹,运行该文件夹下的startup.bat文件即可开启服务器,然后在Tomcat的安装目录下打开webapps文件夹,将NewsInfo.xml文件放置在root文件夹下,代码如下:
<?xml version="1.0"?>
-<news>
-<newsInfo>
<icon>localhost:8080/img/b.png</icon>
<title>《神武》</title>
<content>新美术资源盘点,视觉新体验</content>
<type>2</type>
<comment>35</comment>
</newsInfo
-<newsInfo>
<icon>localhost:8080/img/c.png</icon>
<title>南北车正式公布合并</title>
<content>南北车将于今日正式公布合并</content>
<type>3</type>
<comment>2</comment>
</newsInfo>
-<newsInfo>
<icon>localhost:8080/img/d.png</icon>
<title>呆了!汪星人抱玩偶酣睡</title>
<content>汪星人抱玩偶酣睡,萌翻网友</content>
<type>1</type>
<comment>25</comment>
</newsInfo>
-<newsInfo>
<icon>localhost:8080/img/e.png</icon>
<title>风力发电进校园</title>
<content>风力发电普进校园</content>
<type>2</type>
<comment>26</comment>
</newsInfo>
-<newsInfo>
<icon>localhost:8080/img/f.png</icon>
<title>地球一小时</title>
<content>地球熄灯一小时</content>
<type>1</type>
<comment>23</comment>
</newsInfo>
-<newsInfo>
<icon>localhost:8080/img/g.png</icon>
<title>最美公路</title>
<content>最美公路,难以想象</content>
<type>1</type>
<comment>23</comment>
</newsInfo>
</news>
七.在value文件夹下创建一个config.xml
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="serverurl">http://10.61.6.212:8080/newInfo.xml</string>
</resources>
八.添加权限
因为需要访问网络因此需要在androidMainfest.xml里面配置相应的权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.edu.bzu.anew">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
九.完成后如下所示:
代码有点小瑕疵,建议看看就好!!
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- c语言实现url的编码和解码
- 排序-堆排序-数据结构(32)
- 交叉验证 cross_validation
- 动态链表与静态链表
- CSS的包裹性
- 新闻客户端
- 关于ajaxForm 和ckeditor连用可是提交的时候不能够获得ckeditor填写的内容的问题
- mui中如何同时监听多个相同的标签
- HDU 1559 最大子矩阵和 (dp) 枚举+dp
- Python学习资源汇总
- ZUFE-无人能解的数学难题
- IAR EWARM Printf via SWO
- 图概论
- linux下cd 和cd /的区别