{"_id":"56cae5309f4ae20b00644dca","project":"568fce2a04440a1700e4cb47","user":"55116f88e2990b0d00fb0552","__v":19,"version":{"_id":"568fce2b04440a1700e4cb4a","project":"568fce2a04440a1700e4cb47","__v":20,"createdAt":"2016-01-08T14:56:43.101Z","releaseDate":"2016-01-08T14:56:43.101Z","categories":["568fce2b04440a1700e4cb4b","568fd1b8b700ce0d002f4b1c","568fd23804440a1700e4cb5b","568fd2444719c119002ce5d8","568ff21204440a1700e4cbc1","5693732c8aa8040d009f2c28","5693738393445b0d00abdad0","5693740093445b0d00abdad1","56937445974aaa0d001ca699","5693b82173f48f0d0075c90d","5694c4cd1005590d0062cb25","569f854466a5640d00efa54c","56a264cdd15dd70d008d825b","56aa56bf318e6c1700a19ddb","56b0e6347ae4550d000627bd","56b200c0f48f270d00e0de6f","56b200c6f48f270d00e0de70","56b22a9665ddf50d0076ba40","56e92ef71996862200fd7f42","574d6577fb835c0e00ca316a"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"parentDoc":null,"category":{"_id":"56e92ef71996862200fd7f42","project":"568fce2a04440a1700e4cb47","__v":0,"version":"568fce2b04440a1700e4cb4a","pages":[],"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-03-16T10:01:27.194Z","from_sync":false,"order":3,"slug":"java-quickstarts","title":"Java Quickstarts"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-02-22T10:38:40.307Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":0,"body":"[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"\",\n  \"body\": \"Full source for these quickstarts can be downloaded from http://download.dynmark.com/Files/API/Quickstart/Java.zip\"\n}\n[/block]\n##Prerequisites\nYou'll need a Dynmark account to be able to send messages. You can use a trial account to send a limited number of free messages for testing.\n\n##Hello World via SMS\nWe're going to create a simple console application that sends a Hello World message when the user enters their phone number.\n\nWe're going to make use of the following libraries\n* [Unirest](http://unirest.io/java.html) to make calling REST APIs easy.\n* [Jackson](http://wiki.fasterxml.com/JacksonHome) for JSON deserialisation\n\n1. Create a new java console project\n2. Add the following libraries to the project\n  * unirest-java-1.4.7.jar\n  * jackson-annotations-2.7.2.jar\n  * jackson-core-2.7.2.jar\n  * jackson-databind-2.7.2.jar\n3. Add a new class, SendSMS.java\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"package com.dynmark;\\n\\nimport java.io.IOException;\\nimport java.util.Scanner;\\n\\nimport com.fasterxml.jackson.databind.ObjectMapper;\\nimport com.mashape.unirest.http.HttpResponse;\\nimport com.mashape.unirest.http.Unirest;\\nimport com.mashape.unirest.http.exceptions.UnirestException;\\n\\npublic class SendSMS {\\n\\tprivate static String userName = \\\":::at:::username@\\\";\\n\\tprivate static String password = \\\"@Password@\\\";\\n\\tprivate static ObjectMapper objectMapper = new ObjectMapper();\\n  \\n  public static void main(String[] args) throws UnirestException, IOException {\\n\\n\\t\\tScanner scanIn = new Scanner(System.in);\\n\\t\\tSystem.out.println(\\\"Enter your phone number: \\\");\\n\\t\\tString phoneNumber = scanIn.nextLine();\\n\\n\\t\\tHttpResponse<String> response = Unirest.post(\\\"https://services.dynmark.com/webapi/message/send\\\")\\n\\t\\t  .header(\\\"content-type\\\", \\\"application/json\\\")\\n\\t\\t  .header(\\\"accept\\\", \\\"application/json\\\")\\n\\t\\t  .basicAuth(userName, password)\\n\\t\\t  .body(\\\"[{\\\"\\n\\t\\t  \\t\\t+ \\\"'from': 'Example',\\\"\\n\\t\\t  \\t\\t+ \\\"'to': '\\\" + phoneNumber + \\\"',\\\"\\n\\t\\t  \\t\\t+ \\\"'body': 'Hello World. This is a test message',\\\"\\n\\t\\t  \\t\\t+ \\\"}]\\\")\\n\\t\\t  .asString();\\n\\t}\\n}\\n\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nLets look at the details:\n1. You'll need your Dynmark username and password to authenticate calls.\n2. We use the [Unirest](http://unirest.io/java.html) library to make REST calls\n3. We define an SMS message request containing the bare minimum details to send an SMS.\n4. The response is returned as a string\n\nUsers often make mistakes entering their details, so next we'll add some code to check the response from the API for validation failures.\n\nFirst we must define 2 additional classes to parse the response JSON into. Add ApiValidationFailure.java\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"package com.dynmark;\\n\\npublic class ApiValidationFailure {\\n\\t\\n\\tprivate String property;\\n\\tprivate String failureCode;\\n\\tprivate String details;\\n\\t\\n\\tpublic String getProperty() {\\n\\t\\treturn property;\\n\\t}\\n\\tpublic void setProperty(String property) {\\n\\t\\tthis.property = property;\\n\\t}\\n\\tpublic String getFailureCode() {\\n\\t\\treturn failureCode;\\n\\t}\\n\\tpublic void setFailureCode(String failureCode) {\\n\\t\\tthis.failureCode = failureCode;\\n\\t}\\n\\tpublic String getDetails() {\\n\\t\\treturn details;\\n\\t}\\n\\tpublic void setDetails(String details) {\\n\\t\\tthis.details = details;\\n\\t}\\n}\\n\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nAdd MessageResponse.java\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"package com.dynmark;\\n\\nimport java.util.ArrayList;\\n\\nimport com.fasterxml.jackson.annotation.JsonIgnoreProperties;\\nimport com.fasterxml.jackson.annotation.JsonProperty;\\n\\n@JsonIgnoreProperties(ignoreUnknown = true)\\npublic class MessageResponse {\\n\\n\\t@JsonProperty(\\\"to\\\")\\n\\tprivate String to;\\n\\t\\n\\t@JsonProperty(\\\"successful\\\")\\n\\tprivate boolean successful;\\n\\t\\n\\t@JsonProperty(\\\"validationFailures\\\")\\n\\tprivate ArrayList<ApiValidationFailure> validationFailures;\\n\\t\\n\\tpublic String getTo() {\\n\\t\\treturn to;\\n\\t}\\n\\tpublic void setTo(String to) {\\n\\t\\tthis.to = to;\\n\\t}\\n\\tpublic boolean isSuccessful() {\\n\\t\\treturn successful;\\n\\t}\\n\\tpublic void setSuccessful(boolean successful) {\\n\\t\\tthis.successful = successful;\\n\\t}\\n\\tpublic ArrayList<ApiValidationFailure> getValidationFailures() {\\n\\t\\treturn validationFailures;\\n\\t}\\n\\tpublic void setValidationFailures(ArrayList<ApiValidationFailure> validationFailures) {\\n\\t\\tthis.validationFailures = validationFailures;\\n\\t}\\n}\\n\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nExtend SendMessage to check for an HTTP status of 202 Accepted\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"        if (response.getStatus() == 202)\\n        {\\n        \\tMessageResponse[] body = objectMapper.readValue(response.getBody(), MessageResponse[].class);\\n\\n        \\tif (body[0].isSuccessful())\\n        \\t{\\n        \\t\\tSystem.out.println(\\\"We sent you a message.\\\");\\n        \\t}\\n        \\telse\\n        \\t{\\n        \\t\\tSystem.out.println(\\\"Sorry, we couldn't send you a message.\\\");\\n        \\t\\tfor (ApiValidationFailure failure : body[0].getValidationFailures())\\n        \\t\\t{\\n        \\t\\t\\tif (failure.getFailureCode().equals(\\\"ToInvalid\\\"))\\n        \\t\\t\\t{\\n        \\t\\t\\t\\tSystem.out.println(\\\"Your phone number doesn't look like a valid number.\\\");\\n        \\t\\t\\t}\\n        \\t\\t}\\n        \\t}\\n        }\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nWe check that the body passed basic validation using the *isSuccessful* property. If this is false, then the message was rejected, with the reasons for rejection returned in the validationFailures property. \n \nWe should also check for HTTP 401 Unauthorised to ensure we're using the right credentials, and also for HTTP 400 Bad Request to ensure we've submitted a request that passes basic JSON validation.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"        else if (response.getStatus() == 401)\\n        {\\n        \\tSystem.out.println(\\\"Your username and password are incorrect\\\");\\n        }\\n        else if (response.getStatus() == 400)\\n        {\\n        \\tSystem.out.println(\\\"Bad request format\\\");\\n        }\\n        else\\n        {\\n    \\t\\t\\tSystem.out.println(\\\"Non success response \\\" + response.getStatus());\\n        }\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]","excerpt":"Sending text messages via the REST API","slug":"qs-java-send-sms-messages","type":"basic","title":"Send SMS Messages"}

Send SMS Messages

Sending text messages via the REST API

[block:callout] { "type": "info", "title": "", "body": "Full source for these quickstarts can be downloaded from http://download.dynmark.com/Files/API/Quickstart/Java.zip" } [/block] ##Prerequisites You'll need a Dynmark account to be able to send messages. You can use a trial account to send a limited number of free messages for testing. ##Hello World via SMS We're going to create a simple console application that sends a Hello World message when the user enters their phone number. We're going to make use of the following libraries * [Unirest](http://unirest.io/java.html) to make calling REST APIs easy. * [Jackson](http://wiki.fasterxml.com/JacksonHome) for JSON deserialisation 1. Create a new java console project 2. Add the following libraries to the project * unirest-java-1.4.7.jar * jackson-annotations-2.7.2.jar * jackson-core-2.7.2.jar * jackson-databind-2.7.2.jar 3. Add a new class, SendSMS.java [block:code] { "codes": [ { "code": "package com.dynmark;\n\nimport java.io.IOException;\nimport java.util.Scanner;\n\nimport com.fasterxml.jackson.databind.ObjectMapper;\nimport com.mashape.unirest.http.HttpResponse;\nimport com.mashape.unirest.http.Unirest;\nimport com.mashape.unirest.http.exceptions.UnirestException;\n\npublic class SendSMS {\n\tprivate static String userName = \"@username@\";\n\tprivate static String password = \"@Password@\";\n\tprivate static ObjectMapper objectMapper = new ObjectMapper();\n \n public static void main(String[] args) throws UnirestException, IOException {\n\n\t\tScanner scanIn = new Scanner(System.in);\n\t\tSystem.out.println(\"Enter your phone number: \");\n\t\tString phoneNumber = scanIn.nextLine();\n\n\t\tHttpResponse<String> response = Unirest.post(\"https://services.dynmark.com/webapi/message/send\")\n\t\t .header(\"content-type\", \"application/json\")\n\t\t .header(\"accept\", \"application/json\")\n\t\t .basicAuth(userName, password)\n\t\t .body(\"[{\"\n\t\t \t\t+ \"'from': 'Example',\"\n\t\t \t\t+ \"'to': '\" + phoneNumber + \"',\"\n\t\t \t\t+ \"'body': 'Hello World. This is a test message',\"\n\t\t \t\t+ \"}]\")\n\t\t .asString();\n\t}\n}\n", "language": "java" } ] } [/block] Lets look at the details: 1. You'll need your Dynmark username and password to authenticate calls. 2. We use the [Unirest](http://unirest.io/java.html) library to make REST calls 3. We define an SMS message request containing the bare minimum details to send an SMS. 4. The response is returned as a string Users often make mistakes entering their details, so next we'll add some code to check the response from the API for validation failures. First we must define 2 additional classes to parse the response JSON into. Add ApiValidationFailure.java [block:code] { "codes": [ { "code": "package com.dynmark;\n\npublic class ApiValidationFailure {\n\t\n\tprivate String property;\n\tprivate String failureCode;\n\tprivate String details;\n\t\n\tpublic String getProperty() {\n\t\treturn property;\n\t}\n\tpublic void setProperty(String property) {\n\t\tthis.property = property;\n\t}\n\tpublic String getFailureCode() {\n\t\treturn failureCode;\n\t}\n\tpublic void setFailureCode(String failureCode) {\n\t\tthis.failureCode = failureCode;\n\t}\n\tpublic String getDetails() {\n\t\treturn details;\n\t}\n\tpublic void setDetails(String details) {\n\t\tthis.details = details;\n\t}\n}\n", "language": "java" } ] } [/block] Add MessageResponse.java [block:code] { "codes": [ { "code": "package com.dynmark;\n\nimport java.util.ArrayList;\n\nimport com.fasterxml.jackson.annotation.JsonIgnoreProperties;\nimport com.fasterxml.jackson.annotation.JsonProperty;\n\n@JsonIgnoreProperties(ignoreUnknown = true)\npublic class MessageResponse {\n\n\t@JsonProperty(\"to\")\n\tprivate String to;\n\t\n\t@JsonProperty(\"successful\")\n\tprivate boolean successful;\n\t\n\t@JsonProperty(\"validationFailures\")\n\tprivate ArrayList<ApiValidationFailure> validationFailures;\n\t\n\tpublic String getTo() {\n\t\treturn to;\n\t}\n\tpublic void setTo(String to) {\n\t\tthis.to = to;\n\t}\n\tpublic boolean isSuccessful() {\n\t\treturn successful;\n\t}\n\tpublic void setSuccessful(boolean successful) {\n\t\tthis.successful = successful;\n\t}\n\tpublic ArrayList<ApiValidationFailure> getValidationFailures() {\n\t\treturn validationFailures;\n\t}\n\tpublic void setValidationFailures(ArrayList<ApiValidationFailure> validationFailures) {\n\t\tthis.validationFailures = validationFailures;\n\t}\n}\n", "language": "java" } ] } [/block] Extend SendMessage to check for an HTTP status of 202 Accepted [block:code] { "codes": [ { "code": " if (response.getStatus() == 202)\n {\n \tMessageResponse[] body = objectMapper.readValue(response.getBody(), MessageResponse[].class);\n\n \tif (body[0].isSuccessful())\n \t{\n \t\tSystem.out.println(\"We sent you a message.\");\n \t}\n \telse\n \t{\n \t\tSystem.out.println(\"Sorry, we couldn't send you a message.\");\n \t\tfor (ApiValidationFailure failure : body[0].getValidationFailures())\n \t\t{\n \t\t\tif (failure.getFailureCode().equals(\"ToInvalid\"))\n \t\t\t{\n \t\t\t\tSystem.out.println(\"Your phone number doesn't look like a valid number.\");\n \t\t\t}\n \t\t}\n \t}\n }", "language": "java" } ] } [/block] We check that the body passed basic validation using the *isSuccessful* property. If this is false, then the message was rejected, with the reasons for rejection returned in the validationFailures property. We should also check for HTTP 401 Unauthorised to ensure we're using the right credentials, and also for HTTP 400 Bad Request to ensure we've submitted a request that passes basic JSON validation. [block:code] { "codes": [ { "code": " else if (response.getStatus() == 401)\n {\n \tSystem.out.println(\"Your username and password are incorrect\");\n }\n else if (response.getStatus() == 400)\n {\n \tSystem.out.println(\"Bad request format\");\n }\n else\n {\n \t\t\tSystem.out.println(\"Non success response \" + response.getStatus());\n }", "language": "java" } ] } [/block]