【移動(dòng)應(yīng)用開發(fā)技術(shù)】如何做一個(gè)簡(jiǎn)易的新聞客戶端_第1頁(yè)
【移動(dòng)應(yīng)用開發(fā)技術(shù)】如何做一個(gè)簡(jiǎn)易的新聞客戶端_第2頁(yè)
【移動(dòng)應(yīng)用開發(fā)技術(shù)】如何做一個(gè)簡(jiǎn)易的新聞客戶端_第3頁(yè)
【移動(dòng)應(yīng)用開發(fā)技術(shù)】如何做一個(gè)簡(jiǎn)易的新聞客戶端_第4頁(yè)
【移動(dòng)應(yīng)用開發(fā)技術(shù)】如何做一個(gè)簡(jiǎn)易的新聞客戶端_第5頁(yè)
已閱讀5頁(yè),還剩7頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

【移動(dòng)應(yīng)用開發(fā)技術(shù)】如何做一個(gè)簡(jiǎn)易的新聞客戶端

1,下載一個(gè)服務(wù)端tomcat下載后開始運(yùn)行,將需要瀏覽的東西,放在webapps-root文件下這里假設(shè)有一個(gè)xml小文件,接下來(lái)就開始上代碼了,在同一個(gè)包下給mainactivity創(chuàng)造兩個(gè)class文件,一個(gè)用來(lái)解析xml文件(解析方式多種,有興趣可以上網(wǎng)查閱資料),一個(gè)用于存放數(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文件長(zhǎng)這樣,將它放進(jìn)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>長(zhǎng)春</city> <temp>10°</temp> <p_w_picpath>01:8080/img/c.jpg</p_w_picpath> <wind>3</wind> <pm250>100</pm250>

</channel> <channel>

<city>沈陽(yáng)</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é)點(diǎn) //[6]具體判斷一下是哪個(gè)標(biāo)簽 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é)束標(biāo)簽 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]準(zhǔn)備listview要顯示的數(shù)據(jù),去服務(wù)器取數(shù)據(jù)進(jìn)行封裝 initListData(); } //準(zhǔn)備listview的數(shù)據(jù) privatevoidinitListData(){ newThread(){ publicvoidrun(){ try{ //[2]去服務(wù)器取數(shù)據(jù)04:8080/weather.xml Stringpath="01:8080/weather.xml";

//小白強(qiáng)烈注意,01使用的是本地ip,至于如何查看本地ip

上網(wǎng)百度 //[2.2]創(chuàng)建URL對(duì)象指定我們要訪問(wèn)的網(wǎng)址(路徑) URLurl=newURL(path); //[2.3]拿到httpurlconnection對(duì)象

用于發(fā)送或者接收數(shù)據(jù)

HttpURLConnection

conn=(HttpURLConnection)url.openConnection(); //[2.4]設(shè)置發(fā)送get請(qǐng)求

conn.setRequestMethod("GET");//get要求大寫

默認(rèn)就是get請(qǐng)求 //[2.5]設(shè)置請(qǐng)求超時(shí)時(shí)間 conn.setConnectTimeout(5000); //[2.6]獲取服務(wù)器返回的狀態(tài)碼

intcode=conn.getResponseCode(); //[2.7]如果code==200說(shuō)明請(qǐng)求成功 if(code==200){ //[2.8]獲取服務(wù)器返回的數(shù)據(jù)

是以流的形式返回的

由于把流轉(zhuǎn)換成字符串是一個(gè)非常常見的操作

所以我抽出一個(gè)工具類(utils) InputStreamin=conn.getInputStream();

newsLists=XmlPasser.parserXml(in); runOnUiThread(newRunnable(){ @Override publicvoidrun(){ //TODO自動(dòng)生成的方法存根 //更新ui把數(shù)據(jù)展示到子線程 lv.setAdapter(newMyAdapter()); } }); } }catch(Exceptione){ //TODO自動(dòng)生成的catch塊 e.printStackTrace(); } };}.start(); } privateclassMyAdapterextendsBaseAdapter{ @Override publicintgetCount(){ //TODO自動(dòng)生成的方法存根 returnnewsLists.size(); } @Override publicObjectgetItem(intarg0){ //TODO自動(dòng)生成的方法存根 returnnull; } @Override publiclonggetItemId(intarg0){ //TODO自動(dòng)生成的方法存根 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+"國(guó)內(nèi)"); break; case2: tv_type.setText("跟帖"); break; case3: tv_type.setText("國(guó)外"); break; } returnview; } } }至此src下的正文部分結(jié)束我們接下來(lái)要來(lái)控制按鈕這些內(nèi)容了,在res-layout下創(chuàng)建一個(gè)子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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論