Android利用Gson解析实现天气预报.docx

上传人:b****6 文档编号:8386203 上传时间:2023-01-31 格式:DOCX 页数:10 大小:334.14KB
下载 相关 举报
Android利用Gson解析实现天气预报.docx_第1页
第1页 / 共10页
Android利用Gson解析实现天气预报.docx_第2页
第2页 / 共10页
Android利用Gson解析实现天气预报.docx_第3页
第3页 / 共10页
Android利用Gson解析实现天气预报.docx_第4页
第4页 / 共10页
Android利用Gson解析实现天气预报.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

Android利用Gson解析实现天气预报.docx

《Android利用Gson解析实现天气预报.docx》由会员分享,可在线阅读,更多相关《Android利用Gson解析实现天气预报.docx(10页珍藏版)》请在冰豆网上搜索。

Android利用Gson解析实现天气预报.docx

Android利用Gson解析实现天气预报

用AndroidStudio新建一个默认工程后,将第一张图片红色标记文件修改到相应代码即可

 

 

name="android.permission.INTERNET"/>

 

packagecom.example.rui.weatherforecast;

importandroid.os.Handler;

importandroid.os.Message;

importandroid.support.v7.app.AppCompatActivity;

importandroid.os.Bundle;

importandroid.widget.EditText;

importandroid.widget.Toast;

importcom.google.gson.Gson;

importjava.io.BufferedReader;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.InputStreamReader;

import.HttpURLConnection;

import.MalformedURLException;

import.ProtocolException;

import.URL;

publicclassMainActivityextendsAppCompatActivity{

EditTextet_city,et_weather,et_highTemp,et_lowTemp;

Weatherinfoweatherinfo;

Weatherinfo.Weatherweather;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

init();

}

/**

*初始化控件并开始事件

*/

publicvoidinit(){

//获取控件

et_city=(EditText)findViewById(R.id.city);

et_weather=(EditText)findViewById(R.id.weather);

et_highTemp=(EditText)findViewById(R.id.highTemp);

et_lowTemp=(EditText)findViewById(R.id.lowTemp);

/**

*新建线程进行查询天气

*/

newThread(){

@Override

publicvoidrun(){

Stringstrurl="

try{

getweathers(strurl);

}catch(MalformedURLExceptione){

e.printStackTrace();

}

}

}.start();

}

/**

*子线程响应处理

*/

HandlermHandler=newHandler(){

@Override

publicvoidhandleMessage(Messagemsg){

super.handleMessage(msg);

if(msg.what==1){

Toast.makeText(MainActivity.this,"天气"+weatherinfo.getWeatherinfo().getCity()+weatherinfo.getWeatherinfo().getTemp1(),Toast.LENGTH_SHORT).show();

et_city.setText(weather.getCity());

et_weather.setText(weather.getWeather());

et_lowTemp.setText(weather.getTemp1());

et_highTemp.setText(weather.getTemp2());

}

}

};

/**

*解析JSON数据转换并赋值到实体类中

*

*@parampath

*@throwsMalformedURLException

*/

publicvoidgetweathers(Stringpath)throwsMalformedURLException{

Stringstrjson=(String)getConnetion(path);

Gsongson=newGson();

weatherinfo=gson.fromJson(strjson,Weatherinfo.class);

weather=weatherinfo.getWeatherinfo();

//完成后发送消息给Handler进行下一个事件

Messagemsg=newMessage();

msg.what=1;

mHandler.sendMessage(msg);

}

/**

*联网获取JSON数据

*

*@parampath

*@return

*@throwsMalformedURLException

*/

publicStringgetConnetion(Stringpath)throwsMalformedURLException{

URLurl=newURL(path);

try{

HttpURLConnectionconnection=(HttpURLConnection)url.openConnection();

connection.setConnectTimeout(5000);

connection.setReadTimeout(5000);

connection.setRequestMethod("GET");

InputStreamReaderinputStreamReader=newInputStreamReader(connection.getInputStream());

BufferedReaderbufferedReader=newBufferedReader(inputStreamReader);

Stringline=bufferedReader.readLine().toString();

bufferedReader.close();

inputStreamReader.close();

returnline;

}catch(ProtocolExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

returnnull;

}

}

packagecom.example.rui.weatherforecast;

/**

*CreatedbyRuion2018/1/2.

*/

publicclassWeatherinfo{

privateWeatherweatherinfo;

publicWeathergetWeatherinfo(){

returnweatherinfo;

}

publicvoidsetWeatherinfo(Weatherweatherinfo){

this.weatherinfo=weatherinfo;

}

publicclassWeather{

privateStringcity;

privateStringcityid;

privateStringtemp1;

privateStringtemp2;

privateStringweather;

privateStringimg1;

privateStringimg2;

privateStringptime;

publicStringgetCity(){

returncity;

}

publicvoidsetCity(Stringcity){

this.city=city;

}

publicStringgetCityid(){

returncityid;

}

publicvoidsetCityid(Stringcityid){

this.cityid=cityid;

}

publicStringgetTemp1(){

returntemp1;

}

publicvoidsetTemp1(Stringtemp1){

this.temp1=temp1;

}

publicStringgetTemp2(){

returntemp2;

}

publicvoidsetTemp2(Stringtemp2){

this.temp2=temp2;

}

publicStringgetWeather(){

returnweather;

}

publicvoidsetWeather(Stringweather){

this.weather=weather;

}

publicStringgetImg1(){

returnimg1;

}

publicvoidsetImg1(Stringimg1){

this.img1=img1;

}

publicStringgetImg2(){

returnimg2;

}

publicvoidsetImg2(Stringimg2){

this.img2=img2;

}

publicStringgetPtime(){

returnptime;

}

publicvoidsetPtime(Stringptime){

this.ptime=ptime;

}

}

}

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

android:

orientation="vertical">

android:

id="@+id/textView1"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="城市:

"/>

-

android:

id="@+id/city"

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

ems="10">

android:

id="@+id/textView2"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="天气:

"/>

android:

id="@+id/weather"

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

ems="10"/>

android:

id="@+id/textView3"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="最高温度:

"/>

android:

id="@+id/highTemp"

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

ems="10"/>

android:

id="@+id/textView4"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="最低温度:

"/>

android:

id="@+id/lowTemp"

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

ems="10"/>

compile'com.google.code.gson:

gson:

2.8.2'

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 成人教育 > 电大

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1