Android利用Gson解析實(shí)現(xiàn)天氣預(yù)報(bào)_第1頁(yè)
Android利用Gson解析實(shí)現(xiàn)天氣預(yù)報(bào)_第2頁(yè)
Android利用Gson解析實(shí)現(xiàn)天氣預(yù)報(bào)_第3頁(yè)
Android利用Gson解析實(shí)現(xiàn)天氣預(yù)報(bào)_第4頁(yè)
Android利用Gson解析實(shí)現(xiàn)天氣預(yù)報(bào)_第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)介

1、用 Android Studio 新建一個(gè)默認(rèn)工程后,將即可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) 關(guān) 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);/*新建線程進(jìn)行查詢天氣*

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();/*子線程響應(yīng)處理*/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)換并賦值到實(shí)體類中*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進(jìn)行下一個(gè)事件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. 本站所有資源如無(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)論