예제 #1
0
  public void sendMessage(Message message) throws JSONException {
    JSONObject json = new JSONObject();
    json.put("text", message.getText());
    json.put("roomId", message.getRoomId());
    if (message.getFileAddress() != null && message.getFileAddress().length() > 0) {
      json.put("file", message.getFileAddress());
    } else {
      json.put("file", "");
    }
    NetworkManager.sendRequest(
        MethodsName.SEND_MESSAGE,
        json,
        new NetworkReceiver() {
          @Override
          public void onResponse(Object response) {
            // todo change message status to sent
            Log.wtf("SEND_MESSAGE", response.toString());
          }

          @Override
          public void onErrorResponse(BerimNetworkException error) {
            // todo show error for message.
            Log.wtf("SEND_MESSAGE", error.getMessage());
          }
        });
    addMessage(message);
  }
예제 #2
0
  public void seenMessagesOnServer(final ArrayList<Message> messages, final boolean retry)
      throws JSONException {
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("status", "seen");
    JSONArray messageIdArray = new JSONArray();
    for (Message message : messages) {
      if (message.getSender().getId() != mMe.getId()) {
        messageIdArray.put(message.getId());
      }
    }
    jsonObject.put("messages", messageIdArray);
    if (messageIdArray.length() == 0) {
      return;
    }
    NetworkManager.sendRequest(
        MethodsName.bULK_CHANGE_MESSAGE_STATUS_GOT,
        jsonObject,
        new NetworkReceiver() {
          @Override
          public void onResponse(Object response) {
            // message seen in server side
          }

          @Override
          public void onErrorResponse(BerimNetworkException error) {
            if (retry) {
              try {
                seenMessagesOnServer(messages, false);
              } catch (JSONException e) {

              }
            }
          }
        });
  }
예제 #3
0
 public void uploadFile(String filePath) {
   if (filePath == null) {
     Toast.makeText(mContext, getString(R.string.an_error_occurred_try_again), Toast.LENGTH_SHORT)
         .show();
     return;
   }
   mProgressDialog.show();
   NetworkManager.uploadFile(
       mContext,
       new File(filePath),
       new FutureCallback<Response<String>>() {
         @Override
         public void onCompleted(Exception e, Response<String> result) {
           mProgressDialog.dismiss();
           if (result == null) {
             Toast.makeText(
                     mContext, getString(R.string.an_error_occurred_try_again), Toast.LENGTH_SHORT)
                 .show();
             e.printStackTrace();
           } else {
             Log.wtf("FILE UPLOADED", result.getResult().toString());
             try {
               JSONObject jsonObject = new JSONObject(result.getResult());
               if (jsonObject.getBoolean("error")) {
                 Toast.makeText(
                         mContext,
                         getString(R.string.an_error_occurred_try_again)
                             + ": "
                             + jsonObject.getString("errorMessage"),
                         Toast.LENGTH_SHORT)
                     .show();
               } else {
                 Message message = new Message();
                 message.setText("file");
                 message.setRoomId(mTalkee.getRoomId());
                 message.setFileAddress(jsonObject.getJSONObject("data").getString("fileAddress"));
                 message.setSender(mMe);
                 message.setStatus(Message.MessageStatus.SENT);
                 message.setDate(String.valueOf(System.currentTimeMillis()));
                 message.setId("not-set");
                 sendMessage(message);
               }
             } catch (JSONException ex) {
             }
           }
         }
       });
 }