版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
【移動應(yīng)用開發(fā)技術(shù)】如何做一個簡易的新聞客戶端
1,下載一個服務(wù)端tomcat下載后開始運行,將需要瀏覽的東西,放在webapps-root文件下這里假設(shè)有一個xml小文件,接下來就開始上代碼了,在同一個包下給mainactivity創(chuàng)造兩個class文件,一個用來解析xml文件(解析方式多種,有興趣可以上網(wǎng)查閱資料),一個用于存放數(shù)據(jù)1,存放數(shù)據(jù):packagecom.example.xinwen;publicclassNews{ privateStringcity; privateStringtemp; privateStringwind; privateStringpm250; privateStringp_w_picpath; publicStringgetCity(){ returncity; } publicvoidsetCity(Stringcity){ this.city=city; } publicStringgetTemp(){ returntemp; } publicvoidsetTemp(Stringtemp){ this.temp=temp; } publicStringgetWind(){ returnwind; } publicvoidsetWind(Stringwind){ this.wind=wind; } publicStringgetPm250(){ returnpm250; } publicvoidsetPm250(Stringpm250){ this.pm250=pm250; } publicStringgetImage(){ returnp_w_picpath; } publicvoidsetImage(Stringp_w_picpath){ this.p_w_picpath=p_w_picpath; } }2,解析xml文件(1)假設(shè)xml文件長這樣,將它放進tomcat里面即可<?xmlversion="1.0"encoding="utf-8"?><weather>
<channel>
<city>北京</city> <temp>25°</temp> <p_w_picpath>01:8080/img/a.jpg</p_w_picpath> <wind>1</wind> <pm250>300</pm250>
</channel> <channel>
<city>鄭州</city> <temp>20°</temp> <p_w_picpath>01:8080/img/b.jpg</p_w_picpath> <wind>2</wind> <pm250>300</pm250>
</channel> <channel>
<city>長春</city> <temp>10°</temp> <p_w_picpath>01:8080/img/c.jpg</p_w_picpath> <wind>3</wind> <pm250>100</pm250>
</channel> <channel>
<city>沈陽</city> <temp>20°</temp> <p_w_picpath>01:8080/img/d.jpg</p_w_picpath> <wind>3</wind> <pm250>50</pm250> </channel></weather>(2)開始解析packagecom.example.xinwen;importjava.io.InputStream;importjava.util.ArrayList;importjava.util.List;importorg.xmlpull.v1.XmlPullParser;importorg.xmlpull.v1.XmlPullParserException;importandroid.util.Xml;publicclassXmlPasser{ //解析xml的業(yè)務(wù)方法 publicstaticList<News>parserXml(InputStreamin)throwsException{ List<News>newsLists=null; Newsnews=null; //獲取xml解析器 XmlPullParserparser=Xml.newPullParser(); //設(shè)置解析器,要解析的內(nèi)容 parser.setInput(in,"utf-8"); //獲取要解析的事件類型 inttype=parser.getEventType(); //不得向下解析 while(type!=XmlPullParser.END_DOCUMENT){ switch(type){ caseXmlPullParser.START_TAG://解析開始節(jié)點 //[6]具體判斷一下是哪個標簽 if("weather".equals(parser.getName())){ newsLists=newArrayList<News>(); }elseif("channel".equals(parser.getName())){ news=newNews(); }elseif("city".equals(parser.getName())){ news.setCity(parser.nextText()); }elseif("temp".equals(parser.getName())){ news.setTemp(parser.nextText()); }elseif("p_w_picpath".equals(parser.getName())){ news.setImage(parser.nextText()); }elseif("wind".equals(parser.getName())){ news.setWind(parser.nextText()); }elseif("pm250".equals(parser.getName())){ news.setPm250(parser.nextText()); } break; caseXmlPullParser.END_TAG://解析結(jié)束標簽 if("channel".equals(parser.getName())){ //把Javabean添加到集合 newsLists.add(news); } break; } //不停向下解析 type=parser.next(); } returnnewsLists; }}3,好了副class文件制作好了,就開始在mainactivity中制作正文了packagecom.example.xinwen;importandroid.os.Bundle;importjava.io.IOException;importjava.io.InputStream;import.HttpURLConnection;import.MalformedURLException;import.ProtocolException;import.URL;importjava.util.List;importandroid.app.Activity;importandroid.view.Menu;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.BaseAdapter;importandroid.widget.ImageView;importandroid.widget.ListView;importandroid.widget.TextView;publicclassMainActivityextendsActivity{ privateList<News>newsLists; privateListViewlv; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv=(ListView)findViewById(R.id.lv); //[2]準備listview要顯示的數(shù)據(jù),去服務(wù)器取數(shù)據(jù)進行封裝 initListData(); } //準備listview的數(shù)據(jù) privatevoidinitListData(){ newThread(){ publicvoidrun(){ try{ //[2]去服務(wù)器取數(shù)據(jù)04:8080/weather.xml Stringpath="01:8080/weather.xml";
//小白強烈注意,01使用的是本地ip,至于如何查看本地ip
上網(wǎng)百度 //[2.2]創(chuàng)建URL對象指定我們要訪問的網(wǎng)址(路徑) URLurl=newURL(path); //[2.3]拿到httpurlconnection對象
用于發(fā)送或者接收數(shù)據(jù)
HttpURLConnection
conn=(HttpURLConnection)url.openConnection(); //[2.4]設(shè)置發(fā)送get請求
conn.setRequestMethod("GET");//get要求大寫
默認就是get請求 //[2.5]設(shè)置請求超時時間 conn.setConnectTimeout(5000); //[2.6]獲取服務(wù)器返回的狀態(tài)碼
intcode=conn.getResponseCode(); //[2.7]如果code==200說明請求成功 if(code==200){ //[2.8]獲取服務(wù)器返回的數(shù)據(jù)
是以流的形式返回的
由于把流轉(zhuǎn)換成字符串是一個非常常見的操作
所以我抽出一個工具類(utils) InputStreamin=conn.getInputStream();
newsLists=XmlPasser.parserXml(in); runOnUiThread(newRunnable(){ @Override publicvoidrun(){ //TODO自動生成的方法存根 //更新ui把數(shù)據(jù)展示到子線程 lv.setAdapter(newMyAdapter()); } }); } }catch(Exceptione){ //TODO自動生成的catch塊 e.printStackTrace(); } };}.start(); } privateclassMyAdapterextendsBaseAdapter{ @Override publicintgetCount(){ //TODO自動生成的方法存根 returnnewsLists.size(); } @Override publicObjectgetItem(intarg0){ //TODO自動生成的方法存根 returnnull; } @Override publiclonggetItemId(intarg0){ //TODO自動生成的方法存根 return0; } @Override publicViewgetView(intposition,ViewconvertView,ViewGroupparent){ Viewview; if(convertView==null){ view=View.inflate(getApplicationContext(),R.layout.item,null); }else{ view=convertView; } //找到控件顯示集合里面的數(shù)據(jù) ImageViewiv_icon=(ImageView)view.findViewById(R.id.iv_icon); TextViewtv_title=(TextView)view.findViewById(R.id.tv_title); TextViewtv_desc=(TextView)view.findViewById(R.id.tv_desc); TextViewtv_type=(TextView)view.findViewById(R.id.tv_type); //展示數(shù)據(jù) tv_title.setText(newsLists.get(position).getCity()); tv_desc.setText(newsLists.get(position).getTemp()); Stringtypee=newsLists.get(position).getWind(); Stringcomment=newsLists.get(position).getPm250(); inttype=Integer.parseInt(typee); switch(type){ case1: tv_type.setText(comment+"國內(nèi)"); break; case2: tv_type.setText("跟帖"); break; case3: tv_type.setText("國外"); break; } returnview; } } }至此src下的正文部分結(jié)束我們接下來要來控制按鈕這些內(nèi)容了,在res-layout下創(chuàng)建一個子xml<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:layout_toRightOf="@+id/iv_icon"
android:ellipsize="end"
android:singleLine="true"
android:text="sadasaddsasdasdada"
android:textColor="#000000"
android:textSize="18sp"/>
<TextView
android:id="@+id/tv_desc"
android:layout_width="match_parent"
android:lay
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 魯人版道德與法治九年級上冊11.1《合同是當事人之間的法律》聽課評課記錄
- 滬教版數(shù)學九年級下冊27.1《圓的基本性質(zhì)》聽評課記錄
- 人教版地理七年級下冊第三節(jié)《撒哈拉以南的非洲》聽課評課記錄1
- 人教版七年級數(shù)學下冊 聽評課記錄5.1.3 第1課時《同位角、內(nèi)錯角、同旁內(nèi)角》
- 蘇科版數(shù)學七年級下冊聽評課記錄7.5多邊形的內(nèi)角和與外角和
- 聽評課記錄表8篇二年級
- 【部編版】道德與法治九年級下冊2.1《推動和平與發(fā)展》聽課評課記錄
- 湘教版數(shù)學七年級下冊《相交直線所成的角》聽評課記錄
- 生產(chǎn)計劃外包合同(2篇)
- 獨生子女合同
- 《工程勘察設(shè)計收費標準》(2002年修訂本)
- 《念奴嬌赤壁懷古》名量教學實錄(特級教師程翔)
- 港股通知識點、港股通開通測評題及答案(全)
- 《直播電商平臺運營》-教案全套 第1-8章 直播電商電商營銷新風口-案例解析拆解典型直播成功秘訣
- 放射性肺炎診治
- 即興口語(姜燕)-課件-即興口語第七章PPT-中國傳媒大學
- 艾默生HipulseUPS操作手冊
- 愛心樹(繪本)
- NPI管理流程(精)
- 色卡 對照表 PANTONE-CMYK
- 海員(船員)體格檢查表
評論
0/150
提交評論