Exemplo n.º 1
0
  public static boolean likeMemoryOnServer(Context context, Like like) {
    String url =
        Constants.URL_MEMORY_UPDATE
            + like.getJourneyId()
            + "/memories/"
            + like.getMemoryLocalId()
            + "/like";

    Map<String, String> params = new HashMap<>();
    params.put("api_key", TJPreferences.getApiKey(context));

    Log.d(TAG, "uploading like with parameters " + params);
    Log.d(TAG, "uploading like with url " + url);

    final RequestFuture<JSONObject> futureRequest = RequestFuture.newFuture();
    CustomJsonRequest jsonRequest =
        new CustomJsonRequest(
            com.android.volley.Request.Method.POST, url, params, futureRequest, futureRequest);

    AppController.getInstance().getRequestQueue().add(jsonRequest);
    try {
      JSONObject response = futureRequest.get(30, TimeUnit.SECONDS);
      Log.d(TAG, "memory liked successfully on server with response " + response);
      like.setIdOnServer(response.getJSONArray("likes").getJSONObject(0).getString("id"));
      /*LikeDataSource.updateLike(like, context);*/
      return true;
    } catch (InterruptedException e) {
      Log.d(TAG, "like couldnot be uploaded InterruptedException");
      e.printStackTrace();
    } catch (ExecutionException e) {
      Log.d(TAG, "like couldnot be uploaded ExecutionException");
      e.printStackTrace();
    } catch (TimeoutException e) {
      Log.d(TAG, "like couldnot be uploaded TimeoutException");
      e.printStackTrace();
    } catch (JSONException e) {
      Log.d(TAG, "like couldnot be parsed although uploaded successfully");
      e.printStackTrace();
    }
    return false;
  }
Exemplo n.º 2
0
  public static boolean unlikeMemoryOnServer(Context context, Like like) {
    String url =
        Constants.URL_MEMORY_UPDATE
            + like.getJourneyId()
            + "/memories/"
            + like.getMemoryLocalId()
            + "/unlike";

    Map<String, String> params = new HashMap<>();
    params.put("api_key", TJPreferences.getApiKey(context));

    Log.d(TAG, "unliking memory with parameters " + params);
    Log.d(TAG, "unliking memory with url " + url);

    final RequestFuture<JSONObject> futureRequest = RequestFuture.newFuture();
    CustomJsonRequest jsonRequest =
        new CustomJsonRequest(
            com.android.volley.Request.Method.PUT, url, params, futureRequest, futureRequest);

    AppController.getInstance().getRequestQueue().add(jsonRequest);
    try {
      JSONObject response = futureRequest.get(30, TimeUnit.SECONDS);
      LikeDataSource.deleteLike(context, like.getId());
      Log.d(TAG, "memory unliked successfully on server with response " + response);
      return true;
    } catch (InterruptedException e) {
      Log.d(TAG, "couldnot unlike InterruptedException");
      e.printStackTrace();
    } catch (ExecutionException e) {
      Log.d(TAG, "couldnot unlike ExecutionException");
      e.printStackTrace();
    } catch (TimeoutException e) {
      Log.d(TAG, "couldnot unlike TimeoutException");
      e.printStackTrace();
    }
    return false;
  }