本博文主要记录一下做一个网易客户端的过程和感受。

  1. 这个APP采用的ListView这个控件,首先我们先在布局文件中定义一个ListView。


  2. <ListViewandroid:id="@+id/lv_news"android:layout_width="match_parent"android:layout_height="match_parent"/>

接下来,就是进行mainactivity代码片段的

packagecom.ytu.neteasy;importjava.io.InputStream;importjava.util.ArrayList;importjava.util.List;importorg.apache.http.HttpResponse;importorg.apache.http.client.HttpClient;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.impl.client.DefaultHttpClient;importorg.xmlpull.v1.XmlPullParser;importandroid.app.Activity;importandroid.os.Bundle;importandroid.os.Handler;importandroid.os.Message;importandroid.util.Log;importandroid.util.Xml;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.BaseAdapter;importandroid.widget.ListView;importandroid.widget.TextView;importandroid.widget.Toast;importcom.loopj.android.image.SmartImageView;importcom.ytu.neteasy.entity.NewsInfo;publicclassMainActivityextendsActivity{privatefinalintSUCCESS=0;privatefinalintERROR=1;ListViewlv_News;List<NewsInfo>newsinfo;privatestaticfinalStringTAG="MainActivity";privateHandlerhandler=newHandler(){/***接受消息*/publicvoidhandleMessage(android.os.Messagemsg){switch(msg.what){caseSUCCESS://访问成功//给ListView列表绑定数据MyAdapteradapter=newMyAdapter();//创建一个适配器newsinfo=(List<NewsInfo>)msg.obj;lv_News.setAdapter(adapter);break;caseERROR:Toast.makeText(MainActivity.this,"当前网络崩溃了",0).show();break;default:break;}};};@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);init();}privatevoidinit(){lv_News=(ListView)findViewById(R.id.lv_news);//抓取新闻数据//getNewsFromNet();newThread(newRunnable(){@Overridepublicvoidrun(){//返回时新闻集合List<NewsInfo>newsList=getNewsFromNet();//绑定数据Messagemsg=newMessage();if(newsList!=null){msg.what=SUCCESS;msg.obj=newsList;}else{msg.what=ERROR;}handler.sendMessage(msg);}}).start();}/***抓取网路数据的方法*@return*/privateList<NewsInfo>getNewsFromNet(){HttpClientclient=null;try{//定义一个客户端client=newDefaultHttpClient();//定义get方法Stringuri="http://10.10.49.166:8080/Web025Server/new.xml";HttpGetget=newHttpGet(uri);//执行请求HttpResponseresponse=client.execute(get);intstatuscode=response.getStatusLine().getStatusCode();if(statuscode==200){InputStreamin=response.getEntity().getContent();List<NewsInfo>newsListInfo=getNewListFromInputStream(in);returnnewsListInfo;}else{Log.i(TAG,"请求失败"+statuscode);}}catch(Exceptione){e.printStackTrace();}finally{if(client!=null){client.getConnectionManager().shutdown();}}returnnull;}//解析xml文件/***从流中解析新闻的集合*@paramin*@return*@throwsException*/privateList<NewsInfo>getNewListFromInputStream(InputStreamin)throwsException{XmlPullParserparser=Xml.newPullParser();//创建一个pull解析器parser.setInput(in,"utf-8");//指定解析流和编码List<NewsInfo>newsListInfo=null;NewsInfonewinfo=null;inteventType=parser.getEventType();while(eventType!=XmlPullParser.END_DOCUMENT){//如果没有到达结尾处,继续循环StringtagName=parser.getName();//获得节点名称switch(eventType){caseXmlPullParser.START_TAG://开始节点<news>if("news".equals(tagName)){newsListInfo=newArrayList<NewsInfo>();}elseif("new".equals(tagName)){newinfo=newNewsInfo();}elseif("title".equals(tagName)){newinfo.setTitle(parser.nextText());}elseif("detail".equals(tagName)){newinfo.setDetail(parser.nextText());}elseif("comment".equals(tagName)){newinfo.setComment(Integer.valueOf(parser.next()));}elseif("image".equals(tagName)){newinfo.setImageUrl(parser.nextText());}break;caseXmlPullParser.END_TAG://结束节点</news>if("new".equals(tagName)){newsListInfo.add(newinfo);}break;default:break;}eventType=parser.next();//取下一个事件类型}returnnewsListInfo;}classMyAdapterextendsBaseAdapter{/***返回列表的总长度*/@OverridepublicintgetCount(){returnnewsinfo.size();}@OverridepublicObjectgetItem(intposition){returnnull;}@OverridepubliclonggetItemId(intposition){return0;}/***返回一个列表的子条目的*/@OverridepublicViewgetView(intposition,ViewconvertView,ViewGroupparent){Viewview=null;if(convertView==null){LayoutInflaterinflater=getLayoutInflater();view=inflater.inflate(R.layout.listview_item,null);}else{view=convertView;}SmartImageViewsmicon=(SmartImageView)view.findViewById(R.id.sm_listview_icon);TextViewtitleview=(TextView)view.findViewById(R.id.sm_listview_title);TextViewdetailview=(TextView)view.findViewById(R.id.sm_listview_detail);TextViewcommentview=(TextView)view.findViewById(R.id.sm_listview_comment);NewsInfonewInfo=newsinfo.get(position);smicon.setImageUrl(newInfo.getImageUrl());titleview.setText(newInfo.getTitle());detailview.setText(newInfo.getDetail());commentview.setText(newInfo.getComment()+"跟帖");returnview;}}}

接下来给大家展示listview的布局文件

<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="5dp"><com.loopj.android.image.SmartImageViewandroid:id="@+id/sm_listview_icon"android:layout_width="100dp"android:layout_height="60dp"/><TextViewandroid:id="@+id/sm_listview_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:singleLine="true"android:text="这是标题"android:layout_marginLeft="3dp"android:layout_toRightOf="@id/sm_listview_icon"android:textSize="17sp"android:textColor="@android:color/black"/><TextViewandroid:id="@+id/sm_listview_detail"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="这是内容"android:layout_toRightOf="@id/sm_listview_icon"android:textSize="14sp"android:textColor="@android:color/darker_gray"android:layout_marginTop="3dp"android:layout_below="@id/sm_listview_title"android:layout_alignLeft="@id/sm_listview_title"/><TextViewandroid:id="@+id/sm_listview_comment"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="12sp"android:textColor="#ff0000"android:layout_alignParentRight="true"android:layout_alignParentBottom="true"/></RelativeLayout>

这个项目主要用到了子线程,网络传输数据,处理输入流,以及XML文件的解析

服务器端的部分xml解析

<?xmlversion="1.0"encoding="UTF-8"?><news><new><title>今日之声:北大雕塑被戴口罩</title><detail>市民:因雾霾起诉环保局;公务员谈"紧日子":坚决不出去.</detail><comment>681</comment><image>http://10.10.49.166:8080/Web025Server/images/2.jpg</image></new><new><title>奥巴马见达赖是装蒜</title><detail>外文局-国际民众认可中国大国地位;法院:"流量清零"未侵权.</detail><comment>1359</comment><image>http://10.10.49.166:8080/Web025Server/images/3.jpg</image></new><new><title>轻松一刻:我要沉迷学习不自拔</title><detail>放假时我醒了不代表我起床了,如今我起床了不代表我醒了!</detail><comment>10116</comment><image>http://10.10.49.166:8080/Web025Server/images/4.jpg</image></new><new><title>男女那些事儿</title><detail>"妈,我在东莞被抓,要2万保释金,快汇钱到xxx!"</detail><comment>10339</comment><image>http://10.10.49.166:8080/Web025Server/images/5.jpg</image></new><new><title>3Q大战宣判:腾讯获赔500万</title><detail>最高法驳回360上诉,维持一审宣判.</detail><comment>6427</comment><image>http://10.10.49.166:8080/Web025Server/images/1.jpg</image></new><new><title>今日之声:北大雕塑被戴口罩</title><detail>市民:因雾霾起诉环保局;公务员谈"紧日子":坚决不出去.</detail><comment>681</comment><image>http://10.10.49.166:8080/Web025Server/images/2.jpg</image></new><new><title>奥巴马见达赖是装蒜</title><detail>外文局:国际民众认可中国大国地位;法院:"流量清零"未侵权.</detail><comment>1359</comment><image>http://10.10.49.166:8080/Web025Server/images/3.jpg</image></new><new><title>轻松一刻:我要沉迷学习不自拔</title><detail>放假时我醒了不代表我起床了,如今我起床了不代表我醒了!</detail><comment>10116</comment><image>http://10.10.49.166:8080/Web025Server/images/4.jpg</image></new><new><title>男女那些事儿</title><detail>"妈,我在东莞被抓,要2万保释金,快汇钱到xxx!"</detail><comment>10339</comment><image>http://10.10.49.166:8080/Web025Server/images/5.jpg</image></new><new><title>3Q大战宣判:腾讯获赔500万</title><detail>最高法驳回360上诉,维持一审宣判.</detail><comment>6427</comment><image>http://10.10.49.166:8080/Web025Server/images/1.jpg</image></new><new><title>今日之声:北大雕塑被戴口罩</title><detail>市民:因雾霾起诉环保局;公务员谈"紧日子":坚决不出去.</detail><comment>681</comment><image>http://10.10.49.166:8080/Web025Server/images/2.jpg</image></new><new><title>奥巴马见达赖是装蒜</title><detail>外文局:国际民众认可中国大国地位;法院:"流量清零"未侵权.</detail><comment>1359</comment><image>http://10.10.49.166:8080/Web025Server/images/3.jpg</image></new><new><title>轻松一刻:我要沉迷学习不自拔</title><detail>放假时我醒了不代表我起床了,如今我起床了不代表我醒了!</detail><comment>10116</comment><image>http://10.10.49.166:8080/Web025Server/images/4.jpg</image></new><new><title>男女那些事儿</title><detail>"妈,我在东莞被抓,要2万保释金,快汇钱到xxx!"</detail><comment>10339</comment><image>http://10.10.49.166:8080/Web025Server/images/5.jpg</image></new></news>


更多相关文章

  1. 你有什么秘密 iPhone全都知道
  2. Android(安卓)Studio开发Groovy
  3. QADDroid—android快速原型框架
  4. 【Android外文翻译 - 02】判断是否可以使用某个Intent
  5. 【Android外文翻译 - 01】避免内存泄漏
  6. Android下载(外文)
  7. Android外文翻译系列 - Android应用程序应该是什么样的?
  8. 在Android平台上开发移动应用程序(毕业设计_外文翻译)
  9. Android Calander Event

随机推荐

  1. mysql中替代null的IFNULL()与COALESCE()
  2. mysql5.7.18安装并修改初始密码的方法
  3. Centos7.3下mysql5.7.18安装并修改初始密
  4. MySQL 5.7 mysql command line client 使
  5. ubuntu mysql 5.6版本的删除/安装/编码配
  6. MySQL加密和解密实例详解
  7. mysql sql语句性能调优简单实例
  8. Python3.6-MySql中插入文件路径,丢失反斜
  9. windows下mysql 5.7版本中修改编码为utf-
  10. Workbench通过远程访问mysql数据库的方法