ANdroid网易客户端
本博文主要记录一下做一个网易客户端的过程和感受。
这个APP采用的ListView这个控件,首先我们先在布局文件中定义一个ListView。
<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.p_w_picpath.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("p_w_picpath".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.p_w_picpath.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><p_w_picpath>http://10.10.49.166:8080/Web025Server/p_w_picpaths/2.jpg</p_w_picpath></new><new><title>奥巴马见达赖是装蒜</title><detail>外文局-国际民众认可中国大国地位;法院:"流量清零"未侵权.</detail><comment>1359</comment><p_w_picpath>http://10.10.49.166:8080/Web025Server/p_w_picpaths/3.jpg</p_w_picpath></new><new><title>轻松一刻:我要沉迷学习不自拔</title><detail>放假时我醒了不代表我起床了,如今我起床了不代表我醒了!</detail><comment>10116</comment><p_w_picpath>http://10.10.49.166:8080/Web025Server/p_w_picpaths/4.jpg</p_w_picpath></new><new><title>男女那些事儿</title><detail>"妈,我在东莞被抓,要2万保释金,快汇钱到xxx!"</detail><comment>10339</comment><p_w_picpath>http://10.10.49.166:8080/Web025Server/p_w_picpaths/5.jpg</p_w_picpath></new><new><title>3Q大战宣判:腾讯获赔500万</title><detail>最高法驳回360上诉,维持一审宣判.</detail><comment>6427</comment><p_w_picpath>http://10.10.49.166:8080/Web025Server/p_w_picpaths/1.jpg</p_w_picpath></new><new><title>今日之声:北大雕塑被戴口罩</title><detail>市民:因雾霾起诉环保局;公务员谈"紧日子":坚决不出去.</detail><comment>681</comment><p_w_picpath>http://10.10.49.166:8080/Web025Server/p_w_picpaths/2.jpg</p_w_picpath></new><new><title>奥巴马见达赖是装蒜</title><detail>外文局:国际民众认可中国大国地位;法院:"流量清零"未侵权.</detail><comment>1359</comment><p_w_picpath>http://10.10.49.166:8080/Web025Server/p_w_picpaths/3.jpg</p_w_picpath></new><new><title>轻松一刻:我要沉迷学习不自拔</title><detail>放假时我醒了不代表我起床了,如今我起床了不代表我醒了!</detail><comment>10116</comment><p_w_picpath>http://10.10.49.166:8080/Web025Server/p_w_picpaths/4.jpg</p_w_picpath></new><new><title>男女那些事儿</title><detail>"妈,我在东莞被抓,要2万保释金,快汇钱到xxx!"</detail><comment>10339</comment><p_w_picpath>http://10.10.49.166:8080/Web025Server/p_w_picpaths/5.jpg</p_w_picpath></new><new><title>3Q大战宣判:腾讯获赔500万</title><detail>最高法驳回360上诉,维持一审宣判.</detail><comment>6427</comment><p_w_picpath>http://10.10.49.166:8080/Web025Server/p_w_picpaths/1.jpg</p_w_picpath></new><new><title>今日之声:北大雕塑被戴口罩</title><detail>市民:因雾霾起诉环保局;公务员谈"紧日子":坚决不出去.</detail><comment>681</comment><p_w_picpath>http://10.10.49.166:8080/Web025Server/p_w_picpaths/2.jpg</p_w_picpath></new><new><title>奥巴马见达赖是装蒜</title><detail>外文局:国际民众认可中国大国地位;法院:"流量清零"未侵权.</detail><comment>1359</comment><p_w_picpath>http://10.10.49.166:8080/Web025Server/p_w_picpaths/3.jpg</p_w_picpath></new><new><title>轻松一刻:我要沉迷学习不自拔</title><detail>放假时我醒了不代表我起床了,如今我起床了不代表我醒了!</detail><comment>10116</comment><p_w_picpath>http://10.10.49.166:8080/Web025Server/p_w_picpaths/4.jpg</p_w_picpath></new><new><title>男女那些事儿</title><detail>"妈,我在东莞被抓,要2万保释金,快汇钱到xxx!"</detail><comment>10339</comment><p_w_picpath>http://10.10.49.166:8080/Web025Server/p_w_picpaths/5.jpg</p_w_picpath></new></news>
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。