1、Android利用Gson解析实现天气预报用Android Studio新建一个默认工程后,将第一张图片红色标记文件修改到相应代码即可package com.example.rui.weatherforecast;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.EditText;import android.widget.Toast;import co
2、m.google.gson.Gson;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import .HttpURLConnection;import .MalformedURLException;import .ProtocolException;import .URL;public class MainActivity extends AppCompatActivity EditText et_city,
3、et_weather, et_highTemp, et_lowTemp;Weatherinfo weatherinfo;Weatherinfo.Weather weather;Overrideprotected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);init();/* * 初始化控件并开始事件 */public void init() /获取控件et_city = (EditText) findViewB
4、yId(R.id.city);et_weather = (EditText) findViewById(R.id.weather);et_highTemp = (EditText) findViewById(R.id.highTemp);et_lowTemp = (EditText) findViewById(R.id.lowTemp);/* * 新建线程进行查询天气 */new Thread() Overridepublic void run() String strurl = try getweathers(strurl); catch (MalformedURLException e)
5、e.printStackTrace(); .start();/* * 子线程响应处理 */Handler mHandler = new Handler() Overridepublic void handleMessage(Message msg) super.handleMessage(msg); if (msg.what = 1) Toast.makeText(MainActivity.this, 天气 + weatherinfo.getWeatherinfo().getCity() + weatherinfo.getWeatherinfo().getTemp1(), Toast.LENG
6、TH_SHORT).show();et_city.setText(weather.getCity();et_weather.setText(weather.getWeather();et_lowTemp.setText(weather.getTemp1();et_highTemp.setText(weather.getTemp2(); ;/* * 解析JSON数据转换并赋值到实体类中 * * param path* throws MalformedURLException */public void getweathers(String path) throws MalformedURLExc
7、eption String strjson = (String) getConnetion(path);Gson gson = new Gson();weatherinfo = gson.fromJson(strjson, Weatherinfo.class);weather = weatherinfo.getWeatherinfo();/完成后发送消息给Handler进行下一个事件Message msg = new Message();msg.what = 1;mHandler.sendMessage(msg);/* * 联网获取JSON数据 * * param path* return*
8、throws MalformedURLException */public String getConnetion(String path) throws MalformedURLException URL url = new URL(path); try HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setConnectTimeout(5000);connection.setReadTimeout(5000);connection.setRequestMethod(GET)
9、;InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream();BufferedReader bufferedReader = new BufferedReader(inputStreamReader);String line = bufferedReader.readLine().toString();bufferedReader.close();inputStreamReader.close(); return line; catch (ProtocolException e)
10、 e.printStackTrace(); catch (IOException e) e.printStackTrace();return null;package com.example.rui.weatherforecast;/* * Created by Rui on 2018/1/2. */public class Weatherinfo private Weather weatherinfo; public Weather getWeatherinfo() return weatherinfo;public void setWeatherinfo(Weather weatherin
11、fo) this.weatherinfo = weatherinfo;public class Weatherprivate String city; private String cityid; private String temp1; private String temp2; private String weather; private String img1; private String img2; private String ptime; public String getCity() return city;public void setCity(String city)
12、this.city = city;public String getCityid() return cityid;public void setCityid(String cityid) this.cityid = cityid;public String getTemp1() return temp1;public void setTemp1(String temp1) this.temp1 = temp1;public String getTemp2() return temp2;public void setTemp2(String temp2) this.temp2 = temp2;p
13、ublic String getWeather() return weather;public void setWeather(String weather) this.weather = weather;public String getImg1() return img1;public void setImg1(String img1) this.img1 = img1;public String getImg2() return img2;public void setImg2(String img2) this.img2 = img2;public String getPtime() return ptime;public void setPtime(String ptime) this.ptime = ptime; -compile com.google.code.gson:gson:2.8.2
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1