

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、用AndroidStudio新建一個默認(rèn)工程后,將即可5'H'AndroidQ|4215app-imanifests蜀AndroidlVIarife&t.xmI巴j白疔白2 匚om.example.rul.weatherforecastnOIVIainActivity0WeatherintoKUcom.example.rui.weatherforecastJandroidTest)3 kbcom.example.rui.weatherforecast僉tt=res巧鯨bdrawabletblayouteoactivityjriain.xirI Eainpmap bval
2、uesGGradleScriptsObuild.gradle(Project:WeatherForecast)Ubuild.gracileModule:appliBperties(CradleVer&ion)胃proguard-rules.pre(ProGuardRulesforapp)需perti&sProjectPrapertiesJOtting&.gradle(ProjectS&ttings)關(guān)pertiesSDKLocation)魚AndroidManiiest.xmImanif
3、est<?xralversion-L0encodin,manifestxnilns:android-http:/s匚hieiELa訂.androidcom/apk/re5/andro1d*package-com.esainple.rui.卿加曲銅尿<uses-perinissionandroidmame-andi_oid,permissimrINTERNET<applic:at-ianandro1d;a11owBackiip-t.rueffandroid:icon-Snipnirip/ic_letunchei-'android:label-WeatherFarecas
4、t8*androldzroumdl匚onfc0tlpmcipfic_riiincher_LouncTandroid:suppertsRtltrue1'5android;theme-©style/AppTheme><actlvityandi_oid:namF,MfiihAgt-ivity用><intent_filter><actionandroid;naines“sndro丄dintent.Baction.WAIIT/>16<cateEoryandroid:name-randi-oid»intent,ceitREory.
5、LAIINCFER/></intent_f11ter</acti¥ity</application21usespermissionandroid:name="android.permission.INTERNET"GMaiinActivityjavaUaiiiictivirypackagecom.exactplemi.weatherforceasi;1+import.publicclassMainActivityextendsAppCompat.Activlty(Edit-Testet_cityret.weather,et.highTeni
6、p,et.lowTemp;更題克烘邊圾weatherinfo;Weatherinfo.Weatherweather;©OverrideprotectedvoidorCreateEinidLesavedlnstanceStaTe)superonCreate(savedlnstanceState):合ContentViewfRrlayout,t?ct2ritv_aj?ain):init0;加*初始化控件并開皓事件packagecom.example.rui.weatherforecast;importandroid.os.Handler;importandroid.os.Message;
7、importandroid.supportv7app.AppCompatActivity;importandroidosBundle;importandroidwidget.EditText;importandroidwidget.Toast;importcomgooglegsonGson;importjavaio.BufferedReader;importjavaio.IOException;importjavaio.InputStream;importjavaio.InputStreamReader;importjavanetHttpURLConnection;.MalformedURLE
8、xception;importjavanetProtocolException;importjavanetURL;publicclassMainActivityextendsAppCompatActivityEditTextet_city,et_weather,et_highTemp,et_lowTemp;Weatherinfoweatherinfo;Weatherinfo.Weatherweather;OverrideprotectedvoidonCreate(BundlesavedlnstanceState)super.onCreate(savedlnstanceState);setCon
9、tentView(R.layout.activity.mail);init();/*初始化控件并開始事件*/publicvoidinit()/獲取控件et_city=(EditText)findViewById(R.id.city);et_weather=(EditText)findViewById(R.id.weathe©;et_highTemp=(EditText)findViewById(R.id.highTemp);et_lowTemp=(EditText)findViewById(R.id.lowTemp);/*新建線程進(jìn)行查詢天氣*/newThread()Override
10、publicvoidrun()Stringstrurl="getweathers(strurl);catch(MalformedURLExceptione)e.printStackTrace();.start();/*子線程響應(yīng)處理*/HandlermHandler=newHandler()OverridepublicvoidhandleMessage(Messagemsg)super.handleMessage(msg);if(msg.what=1)Toast.makeText(MainActivity.this,"天氣"+weatherinfo.getWeat
11、herinfo()getCity()+weatherinfo.getWeatherinfo()getTempl()ToastLENGTH_SHOR®.show();et_city.setText(weather.getCity();et_weather.setText(weather.getWeather();et_lowTemp.setText(weather.getTemp1();et_highTemp.setText(weather.getTemp2();/*解析JSON數(shù)據(jù)轉(zhuǎn)換并賦值到實(shí)體類中* parampath* throwsMalformedURLException*/
12、publicvoidgetweathers(Stringpath)throwsMalformedURLExceptionStringstrjson=(String)getConnetion(path);Gsongson=newGson();weatherinfo=gson.fromJson(strjson,Weatherinfo.class);weather=weatherinfo.getWeatherinfo();/完成后發(fā)送消息給Handler進(jìn)行下一個事件Messagemsg=newMessage();msg.what=1;mHandler.sendMessage(msg);/*聯(lián)網(wǎng)獲取
13、JSON數(shù)據(jù)*parampath* return* throwsMalformedURLException*/publicStringgetConnetion(Stringpath)throwsMalformedURLExceptionURLurl=newURL(path);tryHttpURLConnectionconnection=(HttpURLConnection)url.openConnection();connection.setConnectTimeout(5000);connection.setReadTimeout(5000);connection.setRequestMet
14、hod("GET");InputStreamReaderinputStreamReader=newInputStreamReader(connection.getInputStream();BufferedReaderbufferedReader=newBufferedReader(inputStreamReader);Stringline=bufferedReader.readLine().toString();bufferedReader.close();inputStreamReader.close();returnline;catch(ProtocolExcepti
15、one)e.printStackTrace();catch(IOExceptione)e.printStackTrace();returnnull;J4-0WeatherinfojavaH'eatherinfosetWeatherinfoO1*CreatdbyJiza曲2018/1/2.5vpublicclassWeatherinfoprivateWeather麟簸戎如;10piLblicleathergeiffearherinfo()retiLcneaLh&rinfcij卜1314亡Publicvoidfather<15this,weathsrinfa-weatheri
16、nfa;|L61Li18publicclassleather19privateStringcity;120privateStringcitvid;priva七曰StrinstEnpl;privateStringtetripS;23privateStringweaiher;privateStringimgl;125prisrateStringimg2;»尸亠packagecom.example.rui.weatherforecast;/*CreatedbyRuion2018/1/2.*/publicclassWeatherinfoprivateWeatherweatherinfo;pu
17、blicWeathergetWeatherinfo()returnweatherinfo;publicvoidsetWeatherinfo(Weatherweatherinfo)this.weatherinfo=weatherinfo;publicclassWeatherprivateStringcity;privateStringcityid;privateStringtempi;privateStringtemp2;privateStringweather;privateStringimgi;privateStringimg2;privateStringptime;publicString
18、getCity()returncity;publicvoidsetCity(Stringcity)this.city=city;publicStringgetCityid()returncityid;publicvoidsetCityid(Stringcityid)this.cityid=cityid;publicStringgetTempi()returntempi;publicvoidsetTempi(Stringtempi)this.tempi=tempi;publicStringgetTemp2()returntemp2;publicvoidsetTemp2(Stringtemp2)t
19、his.temp2=temp2;publicStringgetWeather()returnweather;publicvoidsetWeather(Stringweather)this.weather=weather;publicStringgetlmgl()returnimgl;publicvoidsetImg1(Stringimgl)this.img1=img1;publicStringgetlmg2()returnimg2;publicvoidsetImg2(Stringimg2)this.img2=img2;publicStringgetPtime()returnptime;publ
20、icvoidsetPtime(Stringptime)this.ptime=ptime;android:ems="10"requestFocus/>/EditTextTextViewandroid:id="+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="天氣:"/><EditTextandroid:id="+id/wea
21、ther"android:layout_width="match_parent"android:layout_height="wrap_content"android:ems="10"/><TextViewandroid:id="+id/textView3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="最高溫度:"
22、;/><EditTextandroid:id="+id/highTemp"android:layout_width="match_parent"android:layout_height="wrap_content"android:ems="10"/><TextViewandroid:id="+id/textView4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="最低溫度:"/><EditTextandroid:id="+id/lowTemp"android:layout_width="match_parent"android:layou
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度廚師技能競賽合作舉辦協(xié)議
- 人力資源招聘事務(wù)文書草案
- 酒店經(jīng)營管理權(quán)合作協(xié)議
- 電商平臺用戶免責(zé)條款協(xié)議
- 工作紀(jì)律修訂內(nèi)容
- 高效會議事務(wù)組織與實(shí)施流程文書
- 公司股東間股權(quán)認(rèn)購及合作開發(fā)協(xié)議表
- 《正弦定理在三角形中的應(yīng)用:高中數(shù)學(xué)教案》
- 三農(nóng)金融服務(wù)平臺建設(shè)方案
- 工作目標(biāo)實(shí)現(xiàn)路徑規(guī)劃
- 2025年三八婦女節(jié)校長致辭-以柔韌破萬鈞以丹心育桃李
- 2025年浙江省建筑安全員C證考試(專職安全員)題庫及答案
- 2025年常州工業(yè)職業(yè)技術(shù)學(xué)院單招職業(yè)技能測試題庫(培優(yōu))
- 化學(xué)實(shí)驗(yàn)室安全職責(zé)分配
- 1.2 讀懂彼此的心 第二課時 課件 2024-2025學(xué)年五年級下冊道德與法治 統(tǒng)編版
- 2018-2022年北京市中考真題數(shù)學(xué)試題匯編:選擇壓軸(第8題)
- 2025年哈爾濱鐵道職業(yè)技術(shù)學(xué)院高職單招語文2018-2024歷年參考題庫頻考點(diǎn)含答案解析
- 2025年貴州黔源電力股份有限公司招聘筆試參考題庫含答案解析
- DZ∕T 0148-2014 水文水井地質(zhì)鉆探規(guī)程(正式版)
- 2024年黑龍江職業(yè)學(xué)院單招職業(yè)技能測試題庫及答案解析
- 大班-數(shù)學(xué)-分禮物-課件(互動版)
評論
0/150
提交評論