private class WeatherData{
private String _weatherDescription;
private Integer _currentTemperature;
private Integer _LowTemperature;
private Integer _highTemperature;git
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()+"℃"); } } }