コード例 #1
0
  public List<Weather> parseForecast(String rawJsonData) throws JsonParseException {

    List<Weather> forecast = null;

    if ("".equals(rawJsonData) || rawJsonData == null) return Collections.emptyList();

    try {
      JSONObject jsonObject = new JSONObject(rawJsonData);
      JSONObject jCity = jsonObject.getJSONObject("city");
      JSONArray jList = jsonObject.getJSONArray("list");

      String name = jCity.getString("name");
      int cityId = jCity.getInt("id");
      String country = jCity.getString("country");
      City city = new City(cityId, name, country);

      forecast = new ArrayList<>();
      for (int i = 0; i < jList.length(); i++) {
        JSONObject jForecastItem = jList.getJSONObject(i);
        long time = jForecastItem.getLong("dt");
        JSONObject jWeather = jForecastItem.getJSONArray("weather").getJSONObject(0);
        int weatherId = jWeather.getInt("id");
        String description = jWeather.getString("description");

        Weather weather = new Weather(weatherId, description, time);

        JSONObject jWind = jForecastItem.getJSONObject("wind");
        double windSpeed = jWind.getDouble("speed");
        double windDegree = jWind.getDouble("deg");
        Wind wind = new Wind(windDegree, windSpeed);

        JSONObject jMain = jForecastItem.getJSONObject("main");
        double temperature = jMain.getDouble("temp");
        double pressure = jMain.getDouble("pressure");
        double humidity = jMain.getDouble("humidity");
        WeatherCondition condition = new WeatherCondition(temperature, pressure, humidity);

        weather.setCondition(condition).setWind(wind).setCity(city);

        forecast.add(weather);
      }
    } catch (JSONException e) {
      Log.e(LOG_TAG, "Parsing error.", e);
    }
    return (forecast == null) ? Collections.<Weather>emptyList() : forecast;
  }
コード例 #2
0
  public Weather parseWeather(String jsonData) throws JsonParseException {
    Weather weather;
    if ("".equals(jsonData)) return null;
    try {
      JSONObject jsonObject = new JSONObject(jsonData);
      JSONObject sys = jsonObject.getJSONObject("sys");
      JSONObject main = jsonObject.getJSONObject("main");
      JSONObject windj = jsonObject.getJSONObject("wind");
      JSONObject weatherObj = jsonObject.getJSONArray("weather").getJSONObject(0);
      String name = jsonObject.getString("name");

      int cityId = jsonObject.getInt("id");
      String cityCountry = sys.getString("country");
      City city = new City(cityId, name, cityCountry);

      double windSpeed = windj.getDouble("speed");
      double windDegree = windj.optDouble("deg", -1d);
      Wind wind = new Wind(windDegree, windSpeed);

      double temperature = main.getDouble("temp");
      double pressure = main.getDouble("pressure");
      double humidity = main.getDouble("humidity");
      WeatherCondition condition = new WeatherCondition(temperature, pressure, humidity);

      long sunrise = sys.getLong("sunrise");
      long sunset = sys.getLong("sunset");
      AdditionalInfo info = new AdditionalInfo(sunrise, sunset);

      int weatherId = weatherObj.getInt("id");
      String description = weatherObj.getString("description");
      weather = new Weather(weatherId, description, new Date().getTime() / 1000);
      weather.setCity(city).setCondition(condition).setInfo(info).setWind(wind);
    } catch (JSONException e) {
      Log.e(LOG_TAG, "Parsing error!", e);
      throw new JsonParseException("Exception during parsing!", e);
    }
    return weather;
  }