Android利用Gson解析实现天气预报_第1页
Android利用Gson解析实现天气预报_第2页
Android利用Gson解析实现天气预报_第3页
Android利用Gson解析实现天气预报_第4页
Android利用Gson解析实现天气预报_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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数据转换并赋值到实体类中*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();/完成后发送消息给Handler进行下一个事件Message msg = new Message();msg.what = 1;mHandler.sendMessage(msg);/*联网获取JSON数据* 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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论