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'