




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、用 Android Studio 新建一個默認工程后,將即可5H AndroidQ | 42 15 app-i manifests蜀 AndroidlVIar ife&t.xm I巴 j白疔白 匚 om.example.rul.weatherforecastnO IVI a in Activity0 WeatherintoK U com.example.rui.weatherforecast JandroidTest)k b com.example.rui.weatherforecast 僉 t t=res巧鯨 b drawablet b layouteo activityjri a in.x
2、ir IEa inpmapb values G Gradle ScriptsO build.gradle (Project: WeatherForecast)Ubuild.gracile Module: appl iBperties (Cradle Ver&ion) 胃 pro guard-rules.pre (ProGuard Rules for app) 需 perti&s Project PrapertiesJ O tting&.gradle (Project S&ttings) 關 perties SDK Location)魚 And ro i d Maniiest.xm Imanif
3、est?xral version- L 0 encodin,manifest xnilns:android-http:/s匚hieiELa訂.android com/apk/re5/andro 1 d* package- com. esainple. rui.卿加曲銅尿 uses-perini ssion androi d mame- andi_oi d, permi ssi mr INTERNET16 /intent_f11 ter/actiity/application21usespermissionandroid:name=android.permission.INTERNETusesp
4、ermissionandroid:name=android.permission.INTERNETG Ma i in Activity javaUaiiiictivirypackage com. exactple mi. weather force a si;1+ impor t .public class Ma inActivity extends AppComp a t.Ac t i vl t y (Edit-Test et_cityr et.weather, et.highTenip, et.lowTemp;更題克烘邊圾 weather info; We a ther info. Wea
5、ther weather;Overrideprotected void orCreateEinidLe savedlnstanceStaTe) super onCreate (savedlnstanceState): 合ContentViewfRr layout, t?ct2ritv_aj?ain): init 0 ;加*初始化控件并開皓事件package com .example .rui.weatherforecast;import android.os.Handler;import android.os.Message;import android .supportv7 app .App
6、CompatActivity;import androidosBundle;import androidwidget.EditText;import androidwidget.Toast;import comgooglegson Gson;import javaio.BufferedReader;import javaio.IOException;import javaio.InputStream;import javaio.InputStreamReader;import javanetHttpURLConnection;import .MalformedURLException;impo
7、rt javanet ProtocolException;import javanetURL;public class MainActivity extends AppCompatActivity EditText et_city, et_weather, et_highTemp, et_lowTemp;Weatherinfo weatherinfo;Weatherinfo. Weather weather;Overrideprotected void onCreate(Bundle savedlnstanceState) super.onCreate(savedlnstanceState);
8、setContentView(R.layout. activity.mail);init();/*初始化控件并開始事件*/public void init () /獲取控件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);/*新建線程進行查詢天氣*
9、/new Thread() Overridepublic void run() String strurl = HYPERLINK /data/cityinfo/101010100.html /data/cityinfo/101010100.html; try getweathers(strurl); catch (MalformedURLException e) e.printStackTrace();.start();/*子線程響應處理*/Handler mHandler = new Handler() Overridepublic void handleMessage(Message m
10、sg) super.handleMessage(msg);if (msg. what = 1) Toast. makeText(MainActivity. this,天氣+weatherin fo.g etWeatherinf o()g etCity() + weatherin fo.g etWeatherinf o()g etTempl() ToastLENGTH_SHOR .show();et_city. setText(weather.getCity(); et_weather.setText(weather.getWeather();et_lowTemp.setText(weather
11、.getTemp1(); et_highTemp .setText(weather.getTemp2();/*解析JSON數(shù)據(jù)轉(zhuǎn)換并賦值到實體類中*param paththrows MalformedURLException*/public void getweathers(String path) throws MalformedURLException String strjson = (String) getConnetion(path);Gson gson = new Gson();weatherinfo = gson.fromJson(strjson, Weatherinfo.cla
12、ss);weather = weatherinfo.getWeatherinfo();/完成后發(fā)送消息給Handler進行下一個事件Message msg = new Message();msg.what = 1;mHandler.sendMessage(msg);/*聯(lián)網(wǎng)獲取JSON數(shù)據(jù)* param pathreturnthrows MalformedURLException*/public String getConnetion(String path) throws MalformedURLException URL url = new URL(path);try HttpURLCon
13、nection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5000);connection.setReadTimeout(5000); connection.setRequestMethod(GET);InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream(); BufferedReader bufferedReader = new BufferedRea
14、der(inputStreamReader);String line = bufferedReader.readLine().toString();bufferedReader.close(); inputStreamReader.close();return line; catch (ProtocolException e) e.printStackTrace(); catch (IOException e) e.printStackTrace();return null;J4- 0 Weatheri nfojavaHeatherinf o se tWeatheri nfoO1* Crea
15、td by Jiza 曲 2018/1/2.5vpublic class Weatherinfo private Weather 麟簸戎如;10piLblic leather geiffearherinfo() retiLcn eaLh&rinfcij 卜1314亡Public voidfather/EditTextTextViewandroid:id=+id/textView2android:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:text=天 氣:/buildTjpes release ( nmu
16、fyEnabled falsd-ependerici es entail onKunnerandroid, support, t-est/runner, AndroiajUniromnerprogiiardFiles gmtDefaultPi-OKuardFi le ( proguard-android. ty.t ), * prDEii?srd-ruleSr pro S2Vdepe口denci 呂忘iffipl&ffientatiDn f ileTree (dir;! ? libs?, ineluds! I +. jar?) implEmeDtation com. andioLcL support: appcocnpai-v?1_ O implementa tion com, ndr oidr support, gqhs郭int; GQnsstreiint-lHyout:; 1, Q, 31 t estlnsp 1 ements
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 建筑項目班組管理制度
- 湖北自考急救試題及答案
- 裝配眼鏡初級試題及答案
- 鋼筆書法試題及答案
- 2025年江蘇省南京玄武區(qū)六校聯(lián)考七下英語期末經(jīng)典模擬試題含答案
- 斑馬進度培訓試題及答案
- 江蘇省行測試題及答案
- 三觀道德考試題及答案
- 恐怖癥心理測試題及答案
- 2025屆四川省巴中學市南江縣八下英語期末質(zhì)量檢測模擬試題含答案
- 2025年普通話水平測試考試試卷及答案
- 2025年地理高考復習 熱點專練 黑吉遼熱點01 傳統(tǒng)文化(解析版)
- 運維自動化流程設計-全面剖析
- 2024北京海淀區(qū)五年級(下)期末語文試題及答案
- 2024年重慶萬州公開招聘社區(qū)工作者考試試題答案解析
- 湖北省武漢華中師大一附中2025年高考沖刺押題(最后一卷)語文試卷含解析
- 錦州師專2025年單招語文試題庫及答案
- 南寧市第十四中學2025年春八下歷史期中測試卷
- 二級注冊計量師題庫附答案2025
- 國家電網(wǎng)有限公司輸變電工程通 用設計(330~750kV輸電線路絕緣子金具串通 用設計分冊)2024版
- 2025年手術授權理論考核試題
評論
0/150
提交評論