{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","params":[],"results":{"codes":[]},"settings":""},"next":{"description":"","pages":[]},"title":"Validate Phone Numbers","type":"basic","slug":"qs-java-validate-phone-numbers","excerpt":"","body":"[block:callout]\n{\n \"type\": \"info\",\n \"body\": \"Full source for these quickstarts can be downloaded from http://download.dynmark.com/Files/API/Quickstart/Java.zip\"\n}\n[/block]\nNow let's build on the Send SMS Messages Quickstart and add checking for dead numbers before sending the SMS. We'll use the [ValidateNumber](doc:rest-pnv-validatenumber) method on the Phone Number Validation API to do this.\n\nFirst, we'll define the response type for the ValidateNumber method.\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"package com.dynmark;\\n\\nimport com.fasterxml.jackson.annotation.JsonIgnoreProperties;\\nimport com.fasterxml.jackson.annotation.JsonProperty;\\n\\n:::at:::JsonIgnoreProperties(ignoreUnknown = true)\\npublic class ValidateNumberResponse {\\n\\t\\n\\[email protected](\\\"NumberStatus\\\")\\n\\tprivate int numberStatus;\\n\\t\\n\\[email protected](\\\"NormalisedNumber\\\")\\n\\tprivate String normalisedNumber;\\n\\t\\n\\tpublic int getNumberStatus() {\\n\\t\\treturn numberStatus;\\n\\t}\\n\\tpublic void setNumberStatus(int numberStatus) {\\n\\t\\tthis.numberStatus = numberStatus;\\n\\t}\\n\\tpublic String getNormalisedNumber() {\\n\\t\\treturn normalisedNumber;\\n\\t}\\n\\tpublic void setNormalisedNumber(String normalisedNumber) {\\n\\t\\tthis.normalisedNumber = normalisedNumber;\\n\\t}\\n}\",\n \"language\": \"java\"\n }\n ]\n}\n[/block]\nNext we'll add a ValidateNumber wrapper function that calls the [ValidateNumber](doc:rest-pnv-validatenumber) method and returns the NumberStatus value:\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"\\tprivate static int ValidateNumber(String phoneNumber) throws UnirestException, IOException {\\n\\t\\tHttpResponse<String> response = \\n\\t\\t\\t\\tUnirest.post(\\\"https://services.dynmark.com/webapi/phonenumbervalidation/validatenumber\\\")\\n\\t\\t\\t\\t.header(\\\"content-type\\\", \\\"application/json\\\")\\n\\t\\t\\t\\t.header(\\\"accept\\\", \\\"application/json\\\")\\n\\t\\t\\t\\t.basicAuth(userName, password)\\n\\t\\t\\t\\t.body(\\\"{\\\"\\n\\t\\t\\t\\t\\t\\t+ \\\"\\\\\\\"Number\\\\\\\": \\\\\\\"\\\" + phoneNumber + \\\"\\\\\\\",\\\"\\n\\t\\t\\t\\t\\t\\t+ \\\"\\\\\\\"IsInternational\\\\\\\": true\\\"\\n\\t\\t\\t\\t\\t\\t+ \\\"}\\\")\\n\\t\\t\\t\\t.asString();\\n\\n\\t\\tif (response.getStatus() == 200)\\n\\t\\t{\\n\\t\\t\\tValidateNumberResponse body = objectMapper.readValue(response.getBody(), ValidateNumberResponse.class);\\n\\n\\t\\t\\treturn body.getNumberStatus(); \\t\\n\\t\\t}\\n\\t\\tif (response.getStatus() == 401)\\n\\t\\t{\\n\\t\\t\\tSystem.out.println(\\\"Your username and password are incorrect\\\");\\n\\t\\t\\treturn 0;\\n\\t\\t}\\n\\t\\telse if (response.getStatus() == 400)\\n\\t\\t{\\n\\t\\t\\tSystem.out.println(\\\"Bad request format\\\");\\n\\t\\t\\treturn 0;\\n\\t\\t}\\n\\t\\telse\\n\\t\\t{\\n\\t\\t\\tSystem.out.println(\\\"Non success response \\\" + response.getStatus());\\n\\t\\t\\treturn 0;\\n\\t\\t}\\n\\t}\",\n \"language\": \"java\"\n }\n ]\n}\n[/block]\nNow we'll wrap the SendMessage code from the [Send SMS Messages](doc:qs-java-send-sms-messages) quickstart in a function\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"\\tprivate static boolean SendMessage(String phoneNumber) throws UnirestException, IOException {\\n\\t\\tHttpResponse<String> response = Unirest.post(\\\"https://services.dynmark.com/webapi/message/send\\\")\\n\\t\\t\\t\\t.header(\\\"content-type\\\", \\\"application/json\\\")\\n\\t\\t\\t\\t.header(\\\"accept\\\", \\\"application/json\\\")\\n\\t\\t\\t\\t.basicAuth(userName, password)\\n\\t\\t\\t\\t.body(\\\"[{\\\"\\n\\t\\t\\t\\t\\t\\t+ \\\"'from': 'Example',\\\"\\n\\t\\t\\t\\t\\t\\t+ \\\"'to': '\\\" + phoneNumber + \\\"',\\\"\\n\\t\\t\\t\\t\\t\\t+ \\\"'body': 'Hello, this is a test message',\\\"\\n\\t\\t\\t\\t\\t\\t+ \\\"}]\\\")\\n\\t\\t\\t\\t.asString();\\n\\n\\t\\tif (response.getStatus() == 202)\\n\\t\\t{\\n\\t\\t\\tMessageResponse[] body = objectMapper.readValue(response.getBody(), MessageResponse[].class);\\n\\n\\t\\t\\tif (body[0].isSuccessful())\\n\\t\\t\\t{\\n\\t\\t\\t\\tSystem.out.println(\\\"We sent you a message.\\\");\\n\\t\\t\\t\\treturn true;\\n\\t\\t\\t}\\n\\t\\t\\telse\\n\\t\\t\\t{\\n\\t\\t\\t\\tSystem.out.println(\\\"Sorry, we couldn't send you a message.\\\");\\n\\t\\t\\t\\tfor (ApiValidationFailure failure : body[0].getValidationFailures())\\n\\t\\t\\t\\t{\\n\\t\\t\\t\\t\\tif (failure.getFailureCode().equals(\\\"ToInvalid\\\"))\\n\\t\\t\\t\\t\\t{\\n\\t\\t\\t\\t\\t\\tSystem.out.println(\\\"Your phone number doesn't look like a valid number.\\\");\\n\\t\\t\\t\\t\\t}\\n\\t\\t\\t\\t}\\n\\t\\t\\t\\treturn false;\\n\\t\\t\\t}\\n\\t\\t}\\n\\t\\telse if (response.getStatus() == 401)\\n\\t\\t{\\n\\t\\t\\tSystem.out.println(\\\"Your username and password are incorrect\\\");\\n\\t\\t\\treturn false;\\n\\t\\t}\\n\\t\\telse if (response.getStatus() == 400)\\n\\t\\t{\\n\\t\\t\\tSystem.out.println(\\\"Bad request format\\\");\\n\\t\\t\\treturn false;\\n\\t\\t}\\n\\t\\telse\\n\\t\\t{\\n\\t\\t\\tSystem.out.println(\\\"Non success response \\\" + response.getStatus());\\n\\t\\t\\treturn false;\\n\\t\\t}\\n\\t}\",\n \"language\": \"java\"\n }\n ]\n}\n[/block]\nFinally, add a *main* function to code to call the *ValidateNumber* function and report back the result before sending the message:\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"\\tpublic static void main(String[] args) throws UnirestException, IOException {\\n\\t\\tScanner scanIn = new Scanner(System.in);\\n\\t\\tSystem.out.println(\\\"Enter your phone number: \\\");\\n\\t\\tString phoneNumber = scanIn.nextLine();\\n\\t\\tscanIn.close();\\n\\n\\t\\tswitch (ValidateNumber(phoneNumber))\\n\\t\\t{\\n\\t\\tcase 2:\\n\\t\\t\\tSystem.out.println(\\\"Your phone is on, we're sending you a message.\\\");\\n\\t\\t\\tSendMessage(phoneNumber);\\n\\t\\t\\tbreak;\\n\\t\\tcase 3:\\n\\t\\t\\tSystem.out.println(\\\"Your phone is off, please switch it on and try again in a few minutes.\\\");\\n\\t\\t\\tbreak;\\n\\t\\tcase 4:\\n\\t\\t\\tSystem.out.println(\\\"Your phone number appears to be dead.\\\");\\n\\t\\t\\tbreak;\\n\\t\\tcase 5:\\n\\t\\t\\tSystem.out.println(\\\"Your phone number isn't on a mobile network.\\\");\\n\\t\\t\\tbreak;\\n\\t\\tcase 6:\\n\\t\\t\\tSystem.out.println(\\\"It looks like you entered something that isn't a phone number.\\\");\\n\\t\\t\\tbreak;\\n\\t\\t}\\n\\t}\",\n \"language\": \"java\"\n }\n ]\n}\n[/block]","updates":[],"order":3,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"56d85fc1c83c5f0b00d288a2","project":"568fce2a04440a1700e4cb47","user":"55116f88e2990b0d00fb0552","__v":17,"category":{"sync":{"isSync":false,"url":""},"pages":[],"title":"Java Quickstarts","slug":"java-quickstarts","order":3,"from_sync":false,"reference":false,"_id":"56e92ef71996862200fd7f42","project":"568fce2a04440a1700e4cb47","createdAt":"2016-03-16T10:01:27.194Z","__v":0,"version":"568fce2b04440a1700e4cb4a"},"version":{"version":"1.0","version_clean":"1.0.0","codename":"","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["568fce2b04440a1700e4cb4b","568fd1b8b700ce0d002f4b1c","568fd23804440a1700e4cb5b","568fd2444719c119002ce5d8","568ff21204440a1700e4cbc1","5693732c8aa8040d009f2c28","5693738393445b0d00abdad0","5693740093445b0d00abdad1","56937445974aaa0d001ca699","5693b82173f48f0d0075c90d","5694c4cd1005590d0062cb25","569f854466a5640d00efa54c","56a264cdd15dd70d008d825b","56aa56bf318e6c1700a19ddb","56b0e6347ae4550d000627bd","56b200c0f48f270d00e0de6f","56b200c6f48f270d00e0de70","56b22a9665ddf50d0076ba40","56e92ef71996862200fd7f42","574d6577fb835c0e00ca316a"],"_id":"568fce2b04440a1700e4cb4a","project":"568fce2a04440a1700e4cb47","__v":20,"createdAt":"2016-01-08T14:56:43.101Z","releaseDate":"2016-01-08T14:56:43.101Z"},"createdAt":"2016-03-03T16:01:05.417Z","parentDoc":null,"githubsync":""}