/**
* 从网络拉取天气数据
*/
private Weather getWeaherData(int areaId){
try {
URL url = new URL("http://v.juhe.cn/xiangji_weather/real_time_weather.php?areaid="+areaId+"&key=b516629c537115aa7331eb2c6bec3214");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
//服务器响应
int code = connection.getResponseCode();
if(code == HttpURLConnection.HTTP_OK){//判断请求是否成功
InputStream is = connection.getInputStream();
String str = StreamTools.readNetWork(is);
if(!TextUtils.isEmpty(str)){
//解析
Gson gson = new Gson();
Weather weather = gson.fromJson(str,Weather.class);
return weather;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}