Sample code for 30+ languages & platforms
Java

WaTrend Send WhatsApp Text

See more WaTrend Examples

Send a WhatsApp text.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

        // Use your actual access token instead of 555555555555555555555555555555
        HttpRequest req = new HttpRequest();
        req.addParam("number", "84933313xxx");
        req.addParam("type", "text");
        req.addParam("message", "This is a test message");
        req.addParam("instance_id", "609ACF283XXXX");
        req.addParam("access_token", "555555555555555555555555555555");

        // Note: The WaTrend online documentation indicate a POST should be used.
        // However, it seems you might actually need to send a GET request.
        // It is unclear.
        // If a GET is neeed, you would just send to the URL w/ query params like this:
        StringBuilder sbUrl = new StringBuilder();
        sbUrl.append("https://app.watrend.com/api/send.php?");
        sbUrl.append(req.getUrlEncodedParams());
        String responseBodyStr = http.quickGetStr(sbUrl.getAsString());
        // The responseBodyStr contains the JSON response from the server..

        req.setHttpVerb("POST");
        req.setContentType("application/x-www-form-urlencoded");

        HttpResponse resp = new HttpResponse();
        try {
            http.httpReq("https://app.watrend.com/api/send.php", req, resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        StringBuilder sbResponseBody = new StringBuilder();
        resp.getBodySb(sbResponseBody);

        int respStatusCode = resp.getStatusCode();
        System.out.println("Response Status Code = " + respStatusCode);
        if (respStatusCode >= 400) {
            System.out.println("Response Header:");
            System.out.println(resp.getHeader());
            System.out.println("Failed.");
            return;
        }

        System.out.println(resp.getBodyStr());

        // Both success and failed responses use 200 status code.

        // A success response contains this JSON in the response body:
        // {"status":"success", ... }

        // A failed response will contain something like this:
        // {"status":"error","message":"License Invalidated"}

        JsonObject jResp = new JsonObject();
        jResp.loadSb(sbResponseBody);

        String status = jResp.stringOf("status");
        System.out.println("status: " + status);
    }
}