[整理]Android开发(二)-Weather App

private class WeatherData{

private String _weatherDescription;

private Integer _currentTemperature;

private Integer _LowTemperature;

private Integer _highTemperature;

    public WeatherData(String weatherDescription,Integer currentTemperature, Integer LowTemperature, Integer highTemperature){
_weatherDescription = weatherDescription;
_currentTemperature = currentTemperature;
_LowTemperature = LowTemperature;
_highTemperature = highTemperature;
} public String getWeatherDescription(){
return _weatherDescription;
} public Integer getCurrentTemperature(){
return _currentTemperature;
} public Integer getLowTemperature(){
return _LowTemperature;
} public Integer getHighTemperature(){
return _highTemperature;
}
} //异步获取天气数据,并更新主UI线程
private class HttpRequestAsyncTask extends AsyncTask{ private WeatherData loadWeatherData(String requestUrl){
WeatherData weatherData = null;
String dataResponse = null; //通过API,获取天气数据
try{
URL url = new URL(requestUrl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection(); //conn.setConnectTimeout(10000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("GET");
conn.setRequestProperty("apikey","XXX"); conn.connect(); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
StringBuffer buffer = new StringBuffer();
String lineBuffer = null; while((lineBuffer=reader.readLine()) != null){
buffer.append(lineBuffer);
} reader.close();
conn.disconnect(); dataResponse = buffer.toString();
}
catch (Exception ex){
ex.printStackTrace();
System.out.println("error->"+ex.getMessage());
} //解析对返回的json数据
/** api return value
* {
"errNum": 0,
"errMsg": "success",
"retData": {
"city": "上海",
"pinyin": "shanghai",
"citycode": "101020100",
"date": "15-06-03",
"time": "11:00",
"postCode": "200000",
"longitude": 121.445,
"latitude": 31.213,
"altitude": "19",
"weather": "阴",
"temp": "25",
"l_tmp": "20",
"h_tmp": "25",
"WD": "东北风",
"WS": "微风(<10m/h)",
"sunrise": "04:50",
"sunset": "18:54"
}
}
* */ try{
JSONTokener jsonParser = new JSONTokener(dataResponse);
JSONObject weatherInfo = (JSONObject)jsonParser.nextValue();
int returnValue = weatherInfo.getInt("errNum"); if(returnValue != 0){
System.out.println( "api get error->" + returnValue);
}
else{
JSONObject jsonData = weatherInfo.getJSONObject("retData");
weatherData = new WeatherData(jsonData.getString("weather"),jsonData.getInt("temp"),jsonData.getInt("l_tmp"),jsonData.getInt("h_tmp"));
}
}
catch (JSONException ex){
ex.printStackTrace();
System.out.println( "api get error->" + ex.getMessage());
} return weatherData;
} @Override
protected Object doInBackground(Object[] params) {
String requestUrl = params[0].toString();
return this.loadWeatherData(requestUrl);
} @Override
protected void onPostExecute(Object o) { if(o==null){
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(MainActivity.this);
alertBuilder.setTitle(R.string.alert_title_load_weather_failed)
.setMessage(R.string.alert_message_load_weather_failed); alertBuilder.create();
}
else{
//super.onPostExecute(o);
WeatherData weatherData = (WeatherData)o; //更新UI
CurrentTempratureTextView.setText(weatherData.getCurrentTemperature()+"℃");
}
}
}
上一篇:OperateParticleWithCodes


下一篇:【BZOJ】【3673】可持久化并查集 & 【3674】可持久化并查集加强版