Java SMS API example
Damit dieses Beispiel funktioniert, müssen Sie die Jackson Library jars zu Ihrem classpath hinzufügen. Wenn Sie Maven verwenden, fügen Sie einfach die folgenden Zeilen in Ihre pom.xml ein. Ansonsten können Sie die Jars (Databind, Core und Annotations) einfach hier herunterladen.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
mapper.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
ObjectNode messages = mapper.createObjectNode();
messages.put("text", "Message via API");
messages.put("type", "alert");
messages.put("sender", "");
ObjectNode recipients = mapper.createObjectNode();
recipients.put("value", "33612345678");
ObjectNode sms = mapper.createObjectNode();
ObjectNode data = mapper.createObjectNode();
ArrayNode numero = mapper.createArrayNode();
ObjectNode gsm = mapper.createObjectNode();
numero.add(recipients);
gsm.set("gsm",numero);
sms.set("message",messages);
sms.set("recipients",gsm);
data.putPOJO("sms",sms);
String payload = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(data);
try {
Main hce = new Main();
String response = hce.post("https://api.smsfactor.com/send", payload);
System.out.println(response);
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
public String post(String postUrl, String data) throws IOException {
URL url = new URL(postUrl);
String token = "your.token";//Your first token must be generated on our plateform at https://my.smsfactor.com/developers/api-tokens
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Authorization","Bearer " + token);
con.setDoOutput(true);
this.sendData(con, data);
return this.read(con.getInputStream());
}
protected void sendData(HttpURLConnection con, String data) throws IOException {
DataOutputStream wr = null;
try {
wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(data);
wr.flush();
wr.close();
} catch(IOException exception) {
throw exception;
} finally {
this.closeQuietly(wr);
}
}
private String read(InputStream is) throws IOException {
BufferedReader in = null;
String inputLine;
StringBuilder body;
try {
in = new BufferedReader(new InputStreamReader(is));
body = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
body.append(inputLine);
}
in.close();
return body.toString();
} catch(IOException ioe) {
throw ioe;
} finally {
this.closeQuietly(in);
}
}
protected void closeQuietly(Closeable closeable) {
try {
if( closeable != null ) {
closeable.close();
}
} catch(IOException ex) {
}
}
}
When to use an SMS API in Java?
Optimize your communications with our SMS API designed for Java applications. By easily integrating SMS into your Java projects, you can simplify and improve your communication processes.
Typical examples include :
- Sending SMS to confirm orders and reservations made via an e-commerce application.
- SMS notifications to inform users of scheduled updates or maintenance on your platform.
- Real-time SMS alerts for incidents and emergencies.
- Mass mailings to promote your products or inform your entire customer and user base.
Why choose SMSFactor's Java SMS API?
Our SMS API for Java guarantees optimum reliability and security with 99.9% uptime. Your data and those of your customers are protected in accordance with RGPD standards! And we implement anti-flood measures to protect your accounts and SMS credits.
With the SMSFactor API, you benefit from dedicated technical support for seamless integration. We help you integrate our API into your Java applications, and offer practical advice and ongoing support.
Our API's advanced features are designed to meet the varied needs of your applications:
- Use of webhooks for efficient management of SMS dispatch data.
- Simulation tools to test your automatic SMS triggers.
- Short links with tracking to maximize recipient engagement.
- Receipt of SMS responses and acknowledgements for accurate tracking.
- Automated unsubscribe management to respect user privacy.