package org.linuxguy.MarketBot;

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;

public class GroupMeNotifier extends Notifier<Comment> implements ResultListener<Comment> {
    private static final String GROUPME_BOT_API = "https://api.groupme.com/v3/bots/post";

    private String mAppName;
    private String mAccessToken;

    public GroupMeNotifier(String appName, String accessToken) {
        mAppName = appName;
        mAccessToken = accessToken;
    }

    @Override
    public void onNewResult(Comment result) {
        try {
            String toPost = getJsonPayloadForComment(result);

            if (!postCommentToGroupMe(toPost)) {
                System.err.println("Failed to post payload");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    private static boolean postCommentToGroupMe(String comment) {
        try {
            URL url = new URL(GROUPME_BOT_API);
            HttpsURLConnection httpcon = (HttpsURLConnection) url.openConnection();
            httpcon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpcon.setRequestMethod("POST");

            httpcon.setDoOutput(true);
            httpcon.connect();

            byte[] outputBytes = comment.getBytes("UTF-8");
            OutputStream os = new BufferedOutputStream(httpcon.getOutputStream());
            os.write(outputBytes);

            os.flush();
            os.close();

            httpcon.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    private String getJsonPayloadForComment(Comment c) throws UnsupportedEncodingException {
        return String.format("{\"text\" : \"%s\", \"bot_id\" : \"%s\"}", Utils.formatComment(mAppName, c), mAccessToken);
    }
}

@@ -1035,24 +1035,68 @@
  comment) %7B%0A
+        HttpsURLConnection httpcon = null;%0A%0A
         try 
@@ -1157,35 +1157,16 @@
         
-HttpsURLConnection 
 httpcon 
@@ -1661,24 +1661,31 @@
         
+return 
 httpcon.
 disconne
@@ -1680,20 +1680,32 @@
 con.
-disconnect()
+getResponseCode() == 202
 ;%0A  
@@ -1808,30 +1808,117 @@
    %7D
-%0A%0A        return true;
+ finally %7B%0A            if ( httpcon != null ) %7B%0A                httpcon.disconnect();%0A            %7D%0A        %7D
 %0A   

Verify that we get a 202 result code