Ejemplo n.º 1
0
  @Override
  protected JSONObject doInBackground(String... params) {
    String jsonString = params[0];

    // get url
    int localCatalogIndex = PrefUtils.getLocalCatalogIndex(activity);
    String paiaUrl =
        Constants.getPaiaUrl(localCatalogIndex)
            + "/core/"
            + PaiaHelper.getInstance().getUsername()
            + "/request?access_token="
            + PaiaHelper.getInstance().getAccessToken();

    JSONObject paiaResponse = new JSONObject();

    try {
      this.setPostParameters(jsonString);
      paiaResponse = this.performRequest(paiaUrl);
    } catch (Exception e) {
      e.printStackTrace();

      this.raiseFailure();
    }

    return paiaResponse;
  }
Ejemplo n.º 2
0
  /**
   * This is where the bulk of our work is done. This function is called in a background thread and
   * should generate a new set of data to be published by the loader.
   */
  @Override
  public List<FeeEntry> loadInBackground() {
    List<FeeEntry> response = new ArrayList<FeeEntry>();

    // get url
    SharedPreferences settings = this.fragment.getActivity().getPreferences(0);
    int spinnerValue = settings.getInt("local_catalog", Constants.LOCAL_CATALOG_DEFAULT);

    String paiaUrl =
        Constants.getPaiaUrl(spinnerValue)
            + "/core/"
            + PaiaHelper.getUsername()
            + "/fees?access_token="
            + PaiaHelper.getAccessToken();
    URLConnectionHelper urlConnectionHelper = new URLConnectionHelper(paiaUrl);

    try {
      // open connection
      urlConnectionHelper.configure();
      urlConnectionHelper.connect(null);

      InputStream inputStream = urlConnectionHelper.getStream();

      String httpResponse = urlConnectionHelper.readStream(inputStream);
      Log.v("PAIA", httpResponse);

      JSONObject paiaResponse;
      try {
        paiaResponse = new JSONObject(httpResponse);
      } catch (JSONException e) {
        return response;
      }

      String sum = paiaResponse.getString("amount");

      JSONArray feeArray = paiaResponse.getJSONArray("fee");
      int feeArrayLength = feeArray.length();
      for (int i = 0; i < feeArrayLength; i++) {
        JSONObject fee = (JSONObject) feeArray.get(i);

        String feeDateString = fee.getString("date");
        SimpleDateFormat simpleDateFormat;

        if (feeDateString.substring(2, 3).equals("-")
            && feeDateString.substring(5, 6).equals("-")) {
          simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.GERMANY);
        } else {
          simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.GERMANY);
        }

        Date feeDate = simpleDateFormat.parse(fee.getString("date"));

        response.add(new FeeEntry(fee.getString("amount"), fee.getString("about"), feeDate, sum));
      }
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();

      this.raiseFailure();
    } finally {
      urlConnectionHelper.disconnect();
    }

    return response;
  }
  /**
   * This is where the bulk of our work is done. This function is called in a background thread and
   * should generate a new set of data to be published by the loader.
   */
  @Override
  public List<LocationsEntry> loadInBackground() {
    List<LocationsEntry> response = new ArrayList<LocationsEntry>();

    SharedPreferences settings = this.fragment.getActivity().getPreferences(0);
    int spinnerValue = settings.getInt("local_catalog", Constants.LOCAL_CATALOG_DEFAULT);

    URLConnectionHelper urlConnectionHelper =
        new URLConnectionHelper(Constants.getLocationUrl(spinnerValue));

    try {
      // open connection
      urlConnectionHelper.configure();
      urlConnectionHelper.connect(null);

      InputStream inputStream = urlConnectionHelper.getStream();

      // starts the query
      inputStream = new BufferedInputStream(urlConnectionHelper.getInputStream());

      String httpResponse = urlConnectionHelper.readStream(inputStream);
      Log.v("URI", httpResponse);

      JSONObject jsonResponse = new JSONObject(httpResponse);

      // search the main entry, this should be the one with the key
      // "http://www.w3.org/ns/org#hasSite"
      JSONObject mainEntry = null;
      Iterator<?> keyIterator = jsonResponse.keys();
      while (keyIterator.hasNext()) {
        String key = (String) keyIterator.next();
        JSONObject jsonContent = (JSONObject) jsonResponse.get(key);

        if (jsonContent.has("http://www.w3.org/ns/org#hasSite")) {
          mainEntry = jsonContent;
        }
      }

      // if we did not found a main entry, try to find one with the uri url as key
      String lookupKey =
          Constants.getLocationUrl(spinnerValue)
              .substring(0, Constants.getLocationUrl(spinnerValue).length() - 12);
      if (jsonResponse.has(lookupKey)) {
        mainEntry = (JSONObject) jsonResponse.get(lookupKey);
      }

      // is there a main entry?
      if (mainEntry != null) {
        // add the main entry to the reponse list
        response.add(this.createLocationFromJSON(mainEntry, jsonResponse));

        if (mainEntry.has("http://www.w3.org/ns/org#hasSite")) {
          // iterate the elements of the "http://www.w3.org/ns/org#hasSite" key, holding all child
          // locations
          JSONArray jsonChildArray = mainEntry.getJSONArray("http://www.w3.org/ns/org#hasSite");

          for (int i = 0; i < jsonChildArray.length(); i++) {
            JSONObject jsonChildContent = (JSONObject) jsonChildArray.get(i);

            // get the uri of the child
            String childUri = jsonChildContent.getString("value");

            // make a new uri request
            URLConnectionHelper childUrlConnectionHelper =
                new URLConnectionHelper(childUri + "?format=json");

            try {
              // open connection
              childUrlConnectionHelper.configure();
              childUrlConnectionHelper.connect(null);

              InputStream childInputStream = childUrlConnectionHelper.getStream();

              // starts the query
              childInputStream = new BufferedInputStream(childUrlConnectionHelper.getInputStream());

              String childHttpResponse = childUrlConnectionHelper.readStream(childInputStream);
              Log.v("URI", childHttpResponse);

              JSONObject childJsonResponse = new JSONObject(childHttpResponse);

              if (childJsonResponse.has(childUri)) {
                JSONObject childJsonContent = (JSONObject) childJsonResponse.get(childUri);

                // add the child entry to the reponse list
                response.add(this.createLocationFromJSON(childJsonContent, childJsonResponse));
              }
            } catch (Exception e) {
              throw e;
            } finally {
              childUrlConnectionHelper.disconnect();
            }
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();

      this.raiseFailure();
    } finally {
      urlConnectionHelper.disconnect();
    }

    return response;
  }