コード例 #1
0
  public void updateFinished(OpenweathermapObject data) {
    String errorMessage = getString(R.string.txt_unknown_error);

    if (data != null) {
      errorMessage = data.getErrorMessage();
    }

    if (errorMessage.isEmpty()) {
      SharedPreferences preferences =
          PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
      Boolean del_old =
          preferences.getBoolean(
              getString(R.string.settings_delete_old_data_key),
              getResources().getBoolean(R.bool.settings_delete_old_data_default));
      Realm realm = null;
      try {
        realm = Realm.getDefaultInstance();
        realm.beginTransaction();
        if (del_old) {
          Long timeStamp = data.getWeatherForecastList().get(0).getTimeStamp();
          RealmResults<ForecastListItem> result =
              realm.where(ForecastListItem.class).lessThan("timeStamp", timeStamp).findAll();
          result.clear();
        }
        realm.copyToRealmOrUpdate(data.getWeatherForecastList());
        realm.commitTransaction();

        SharedPreferences.Editor editor = preferences.edit();
        editor.putLong(getString(R.string.last_update_key), System.currentTimeMillis());
        String city = data.getCityName().concat(", ".concat(data.getCountry()));
        editor.putString(getString(R.string.current_city_key), city);
        editor.commit();

      } finally {
        if (realm != null) {
          realm.close();
        }
      }
    }
    Intent intent = new Intent(BROADCAST_ACTION);
    intent.putExtra(PARAM_STATUS, STATUS_FINISH);
    intent.putExtra(PARAM_RESULT, errorMessage);
    sendBroadcast(intent);
  }
コード例 #2
0
  private void updateWeatherForecast(String aCity, String apiId, String aUnits, String language) {
    updateProgress(0);
    OpenweathermapObject openweathermapResult = null;

    String city = aCity;
    if (city.matches("\\d*")) {
      city = "cityId=".concat(city);
    } else {
      city = "q=".concat(city);
    }
    String apiKey = API_KEY.concat(apiId);
    String units = aUnits;
    if (!units.isEmpty()) {
      units = UNITS_KEY.concat(units);
    }
    String lang = language;
    if (!lang.isEmpty()) {
      lang = LANG_KEY.concat(lang);
    }
    String url = BASE_URL.concat(city.concat(apiKey).concat(units.concat(lang)));

    try {
      HttpURLConnection connection = (HttpURLConnection) new java.net.URL(url).openConnection();
      connection.setRequestMethod("GET");
      connection.setReadTimeout(10000);
      connection.connect();
      InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream());

      updateProgress(10);

      Gson gson =
          new GsonBuilder()
              .setExclusionStrategies(
                  new ExclusionStrategy() {
                    @Override
                    public boolean shouldSkipField(FieldAttributes f) {
                      return f.getDeclaringClass().equals(RealmObject.class);
                    }

                    @Override
                    public boolean shouldSkipClass(Class<?> clazz) {
                      return false;
                    }
                  })
              .create();
      openweathermapResult = gson.fromJson(inputStreamReader, OpenweathermapObject.class);
      connection.disconnect();
      connection = null;

      updateProgress(50);

      for (final ForecastListItem item : openweathermapResult.getWeatherForecastList()) {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        String iconName = item.getWeather().get(0).getIconName();
        String iconUrl = IMG_URL.concat(iconName.concat(IMG_EXT));
        try {
          InputStream inputStream = new java.net.URL(iconUrl).openStream();
          byte[] data = new byte[1024];
          int count;
          while ((count = inputStream.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, count);
          }
          buffer.flush();
        } catch (IOException e) {
          e.printStackTrace();
        }
        item.getWeather().get(0).setIconData(buffer.toByteArray());
      }

      updateProgress(99);
      openweathermapResult.setErrorMessage("");

    } catch (IOException e) {
      if (openweathermapResult != null) {
        openweathermapResult.setErrorMessage(e.getMessage());
      }
      e.printStackTrace();
    }

    updateProgress(100);
    updateFinished(openweathermapResult);
  }