Пример #1
0
 public static String extractContent(String url) {
   try {
     Connection connection = Jsoup.connect(url);
     connection.userAgent(USER_AGENT);
     connection.followRedirects(true);
     connection.timeout(GET_TIMEOUT);
     long start = System.currentTimeMillis();
     Connection.Response response = connection.execute();
     long diff = System.currentTimeMillis() - start;
     int responseCode = response.statusCode();
     if (response.statusCode() == OK) {
       String body = response.body();
       Logger.info(
           "%s retrieved, content length %d, time %s sec.",
           url, body.length(), FormatUtil.millis2Seconds(diff));
       return response.body();
     } else {
       Logger.error("%s returned %d", url, responseCode);
       return "";
     }
   } catch (IOException e) {
     Logger.error(e, "%s cannot be read.", url);
     return "";
   }
 }
  @Override
  protected void onPostExecute(Connection.Response response) {
    if (response != null && response.statusCode() == 200) {
      try {
        Log.v(TAG, "Response to JSON request: " + response.body());
        JSONObject root = new JSONObject(response.body());

        boolean success = "success".equals(root.getString("type"));
        int points = root.getInt("points");

        IHasEnterableGiveaways fragment = getFragment();
        fragment.onEnterLeaveResult(giveawayId, getWhat(), success, true);

        // Update the points we have.
        SteamGiftsUserData.getCurrent().setPoints(points);

        if (fragment instanceof Fragment
            && "error".equals(root.getString("type"))
            && "Sync Required".equals(root.getString("msg"))) {
          ((Fragment) fragment)
              .getActivity()
              .startActivity(new Intent(((Fragment) fragment).getContext(), SyncActivity.class));
        }

        return;
      } catch (JSONException e) {
        Log.e(TAG, "Failed to parse JSON object", e);
      }
    }

    getFragment().onEnterLeaveResult(giveawayId, getWhat(), null, false);
  }