1.创建程序
创建一个名为“新闻客户端”的应用程序,包名为cn.edu.bzu;
2.编写用户交互界面
(1)布局文件(activity_main.xml)
界面包含了提示用户正在加载中的ProgressBar、TextView以及用于展示信息的ListView;
新闻客户端界面:
代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/loading" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:visibility="invisible" > <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:id="@+id/lv_news" android:layout_width="match_parent" android:layout_height="match_parent" /> FrameLayout> LinearLayout> (2)ListView的布局文件(new_item.xml)
该布局使用了自定义控件SmartImageView和三个TextView分别用于展示新闻标题、内容以及标题数;
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="65dip" > <com.loopj.android.image.SmartImageView android:id="@+id/siv_icon" android:layout_width="80dip" android:layout_height="60dip" android:layout_alignParentLeft="true" android:layout_marginBottom="5dip" android:layout_marginLeft="5dip" android:layout_marginTop="5dip" android:scaleType="centerCrop" android:src="@drawable/ic_launcher" > com.loopj.android.image.SmartImageView> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dip" android:layout_marginTop="10dip" android:layout_toRightOf="@id/siv_icon" 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="5dip" android:layout_marginTop="5dip" android:layout_toRightOf="@id/siv_icon" 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="5dip" android:text="评论" android:textColor="#99000000" android:textSize="12sp" /> RelativeLayout>
3.界面交互代码:(MainActivity)
用于实现获取服务器的NewInfo.xml文件解析的信息设置到ListView显示界面上,
代码如下:
package cn.edu.bzu.news; import java.io.ByteArrayInputStream; import java.util.List; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.util.Log; 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 cn.edu.bzu.news.bean.NewsInfo; import cn.edu.bzu.news.utils.NewsInfoService; public class MainActivity extends Activity { private ListView lv_news; private LinearLayout loading; private ListnewsInfos; //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.news_item, null); SmartImageView siv = (SmartImageView) view .findViewById(R.id.siv_icon); 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.ic_launcher, R.drawable.ic_launcher); //设置新闻标题 tv_title.setText(newsInfo.getTitle()); //设置新闻描述 tv_description.setText(newsInfo.getDescription()); int type = newsInfo.getType(); 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; } } 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(MainActivity.this.getString(R.string.serverurl), new AsyncHttpResponseHandler() { public void onSuccess(String content) { //访问成功 super.onSuccess(content); byte[] bytes = content.getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream( bytes); newsInfos = NewsInfoService.getNewsInfos(bais); if (newsInfos == null) { Toast.makeText(MainActivity.this, "解析失败",0).show(); } else { // 更新界面 loading.setVisibility(View.INVISIBLE); lv_news.setAdapter(new NewsAdapter()); } } //请求失败 public void onFailure(Throwable error, String content) { super.onFailure(error, content); Log.e("error",MainActivity.this.getString(R.string.serverurl)); Log.e("error", error.toString()); Toast.makeText(MainActivity.this, "请求失败", 0).show(); } }); } }
4.NewsInfo类
NewsInfo对象是新闻信息的JavaBean
定义了iconPath图片路径、title新闻标题、description新闻描述、type新闻类型和comment新闻评论数五种属性。
代码如下:
package cn.edu.bzu.news.bean; 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; } } 5.工具类
用于解析xml文件,使用XmlPulParser对象解析出xml里面的内容并设置到相应的JavaBean中;
在getNewInfo()方法中创建一个List集合newsInfo,解析出来的新闻数据都存放在该集合中。
package cn.itcast.news.utils; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import android.util.Xml; import cn.itcast.news.bean.NewsInfo; public class NewsInfoService { //解析服务器返回的xml信息 获取所有新闻数据实体. public static ListgetNewsInfos(InputStream is) { //获取XmlPullParser对象 XmlPullParser parser = Xml.newPullParser(); try { parser.setInput(is, "utf-8"); //获取指针 int type = parser.getEventType(); List 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 (); } 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.setDescription(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; } } }
6.配置服务器
开启tomcat服务器,在tomcat的安装目录打开webapps文件夹,将NewInfo.xml文件放置在ROOT文件夹下;
代码如下:
xml version="1.0" encoding="utf-8"?> <news> <newsInfo> <icon>http://172.16.25.13.:8080/img/a.jpgicon> <title>科技温暖世界title> <content>进入一个更有爱的领域content> <type>type> <comment>69comment> newsInfo> news>
注:可多添加几个7.添加权限
代码如下:
8.运行程序
使用AsyncHttpClient和SmartImageg View成功地把服务器中的XML数据加载带界面上。
注:该应用程序用eclipse实现,Android编程实现不了;