BicepBuddyApp/src/main/java/Messaging/DMController.java
@SuppressWarnings("unchecked")
	public void sendMessage(Message msg) {
		this.setNumMessages(this.getNumMessages() + 1);
		JSONObject chat = new JSONObject();
		
		chat.put("userId", msg.getSender());
		chat.put("otherUserId", msg.getReceiver());
		chat.put("message", msg.getText());
		
		HttpPost request = new HttpPost(baseUrl);
		try {
			// Add JSON to the body and headers indicating type
			StringEntity params = new StringEntity(chat.toJSONString());
		    request.addHeader("content-type", "application/json");
		    request.setEntity(params);

		    // Execute the request
		    HttpResponse response = httpClient.execute(request);

		    // Get the body of the response
		    HttpEntity entity = response.getEntity();
		    String json = EntityUtils.toString(entity, StandardCharsets.UTF_8);

		    JSONParser parse = new JSONParser();
		    JSONObject o = (JSONObject) parse.parse(json);
		    
		    if(response.getStatusLine().getStatusCode() == 201) {
		    	JSONObject chatter = ((JSONObject) o.get("chat"));
		    	
		    	String id = (String) chatter.get("_id");
		    	msg.setId(id);
		    			    	
		    }
		    else {
		    	Master.appLogger.info("" + response.getStatusLine().getStatusCode());
		    }
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		finally {
			request.releaseConnection();
		}
		
		dm.add(msg);
	}
/**
	 * Send message and add it to the database.
	 *
	 * @param msg the msg
	 */
Send message and add it to the database.
@param msg the msg
Send message and add it to the database . @param msg the msg
@SuppressWarnings("unchecked")
	public void sendMessage(Message msg) {
		this.setNumMessages(this.getNumMessages() + 1);
		JSONObject chat = new JSONObject();
		chat.put("userId", msg.getSender());
		chat.put("otherUserId", msg.getReceiver());
		chat.put("message", msg.getText());
		HttpPost request = new HttpPost(baseUrl);
		try {
			StringEntity params = new StringEntity(chat.toJSONString());
		    request.addHeader("content-type", "application/json");
		    request.setEntity(params);
		    HttpResponse response = httpClient.execute(request);
		    HttpEntity entity = response.getEntity();
		    String json = EntityUtils.toString(entity, StandardCharsets.UTF_8);
		    JSONParser parse = new JSONParser();
		    JSONObject o = (JSONObject) parse.parse(json);
		    if(response.getStatusLine().getStatusCode() == 201) {
		    	JSONObject chatter = ((JSONObject) o.get("chat"));
		    	String id = (String) chatter.get("_id");
		    	msg.setId(id);
		    }
		    else {
		    	Master.appLogger.info("" + response.getStatusLine().getStatusCode());
		    }
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		finally {
			request.releaseConnection();
		}
		dm.add(msg);
	}
@ SuppressWarnings ( "unchecked" ) public void sendMessage ( Message msg ) { this . setNumMessages ( this . getNumMessages ( ) + 1 ) ; JSONObject chat = new JSONObject ( ) ; chat . put ( "userId" , msg . getSender ( ) ) ; chat . put ( "otherUserId" , msg . getReceiver ( ) ) ; chat . put ( "message" , msg . getText ( ) ) ; HttpPost request = new HttpPost ( baseUrl ) ; try { StringEntity params = new StringEntity ( chat . toJSONString ( ) ) ; request . addHeader ( "content-type" , "application/json" ) ; request . setEntity ( params ) ; HttpResponse response = httpClient . execute ( request ) ; HttpEntity entity = response . getEntity ( ) ; String json = EntityUtils . toString ( entity , StandardCharsets . UTF_8 ) ; JSONParser parse = new JSONParser ( ) ; JSONObject o = ( JSONObject ) parse . parse ( json ) ; if ( response . getStatusLine ( ) . getStatusCode ( ) == 201 ) { JSONObject chatter = ( ( JSONObject ) o . get ( "chat" ) ) ; String id = ( String ) chatter . get ( "_id" ) ; msg . setId ( id ) ; } else { Master . appLogger . info ( "" + response . getStatusLine ( ) . getStatusCode ( ) ) ; } } catch ( Exception e ) { e . printStackTrace ( ) ; } finally { request . releaseConnection ( ) ; } dm . add ( msg ) ; }
Send message and add it to the database.
Send message and add it to the database .
// Add JSON to the body and headers indicating type // Execute the request // Get the body of the response