AndroidでSMSを送信
AndroidでHTTPを使用してSMSを送信します
Android : APIドキュメント
AndroidでSMSを送信
Este método le permite enviar SMS a un destinatario utilizando Android. Enviar un SMS con Android es tan sencillo como enviar el origin, destino y el mensaje a enviar a través de una petición a nuestra API.
String afilnet_class="sms";
String afilnet_method="sendsms";
String afilnet_user="user";
String afilnet_password="password";
String afilnet_from="Afilnet";
String afilnet_to="34600000000";
String afilnet_sms="sms+test";
String afilnet_scheduledatetime="";
String afilnet_output="";
// Create an URL request
String sUrl = "https://www.afilnet.com/api/http/?class="+afilnet_class+"&method="+afilnet_method+"&user="+afilnet_user+"&password="+afilnet_password+"&from="+afilnet_from+"&to="+afilnet_to+"&sms="+afilnet_sms+"&scheduledatetime="+afilnet_scheduledatetime+"&output="+afilnet_output;
URL url = new URL(sUrl);
StringBuilder builder = new StringBuilder();
BufferedReader theJSONline = new BufferedReader(new InputStreamReader(url.openStream()));
builder.append(theJSONline.readLine());
String content = builder.toString();
String afilnet_class="sms";
String afilnet_method="sendsms";
String afilnet_user="user";
String afilnet_password="password";
String afilnet_from="Afilnet";
String afilnet_to="34600000000";
String afilnet_sms="sms+test";
String afilnet_scheduledatetime="";
String afilnet_output="";
// Create the POST request
String post = "class="+afilnet_class+"&method="+afilnet_method+"&user="+afilnet_user+"&password="+afilnet_password+"&from="+afilnet_from+"&to="+afilnet_to+"&sms="+afilnet_sms+"&scheduledatetime="+afilnet_scheduledatetime+"&output="+afilnet_output;
// We generate the URL
URL myurl = new URL("https://www.afilnet.com/api/http/");
// We create the connection
HttpURLConnection con = (HttpURLConnection) myurl.openConnection();
con.setDoOutput(true);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("Content-Length", String.valueOf(post.length()));
con.setRequestMethod("POST");
// We build the
OutputStream os = con.getOutputStream();
os.write(post.getBytes());
os.close();
StringBuilder sb = new StringBuilder();
int HttpResult = con.getResponseCode();
if(HttpResult == HttpURLConnection.HTTP_OK)
{
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
System.out.println(""+sb.toString());
} else {
System.out.println(con.getResponseCode());
System.out.println(con.getResponseMessage());
}
String afilnet_class="sms";
String afilnet_method="sendsms";
String afilnet_user="user";
String afilnet_password="password";
String afilnet_from="Afilnet";
String afilnet_to="34600000000";
String afilnet_sms="sms+test";
String afilnet_scheduledatetime="";
String afilnet_output="";
// Create the POST request
String post = "class="+afilnet_class+"&method="+afilnet_method+"&from="+afilnet_from+"&to="+afilnet_to+"&sms="+afilnet_sms+"&scheduledatetime="+afilnet_scheduledatetime+"&output="+afilnet_output;
// We generate the URL
URL myurl = new URL("https://www.afilnet.com/api/basic/");
// We create the connection
HttpURLConnection con = (HttpURLConnection) myurl.openConnection();
String encoded = Base64.getEncoder().encodeToString((afilnet_user+":"+afilnet_password).getBytes(StandardCharsets.UTF_8));
con.setRequestProperty("Authorization", "Basic "+encoded);
con.setDoOutput(true);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("Content-Length", String.valueOf(post.length()));
con.setRequestMethod("POST");
// We build the
OutputStream os = con.getOutputStream();
os.write(post.getBytes());
os.close();
StringBuilder sb = new StringBuilder();
int HttpResult = con.getResponseCode();
if(HttpResult == HttpURLConnection.HTTP_OK)
{
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
System.out.println(""+sb.toString());
} else {
System.out.println(con.getResponseCode());
System.out.println(con.getResponseMessage());
}
パラメータ | 説明 | 必須/オプション |
---|---|---|
class=sms | 要求されたクラス:要求が行われるクラス | 強制的 |
method=sendsms | 要求されたクラスメソッド:要求が行われるクラスのメソッド | 強制的 |
user | Afilnetアカウントのユーザーと電子メール | 強制的 |
password | Afilnetアカウントのパスワード | 強制的 |
from | 送信者 | 強制的 |
to | 受信者の携帯電話番号 | 強制的 |
sms | 送信されるSMSメッセージ | 強制的 |
scheduledatetime | yyyy-mm-dd hh:mm:ss形式での日付と時刻の送信 | オプショナル |
output | 結果の出力形式 | オプショナル |
リクエストを行うと、次のフィールドが表示されます。
- status
-
結果(ステータス=成功の場合)、ここで次の値を受け取ります。
- messageid
- credits
- エラー(ステータス=エラーの場合)、ここでエラーコードを受け取ります
考えられるエラーコードは以下のとおりです。
コード | 説明 |
---|---|
MISSING_USER | ユーザーまたはメールは含まれていません |
MISSING_PASSWORD | パスワードは含まれていません |
MISSING_CLASS | 含まれていないクラス |
MISSING_METHOD | 含まれていないメソッド |
MISSING_COMPULSORY_PARAM | 必須パラメーターは含まれていません |
INCORRECT_USER_PASSWORD | 間違ったユーザーまたはパスワード |
INCORRECT_CLASS | 間違ったクラス |
INCORRECT_METHOD | 間違った方法 |
NO_ROUTE_AVAILABLE | 指定された宛先に使用可能なパスがありません |
NO_CREDITS | 残高が不足しています |
パラメーター:
- class : sms
- method : sendsms
- user : user
- password : password
- from : Afilnet
- to : 34600000000
- sms : sms+test
- scheduledatetime :
- output :
要求:
https://www.afilnet.com/api/http/?class=sms&method=sendsms&user=user&password=password&from=Afilnet&to=34600000000&sms=sms+test&scheduledatetime=&output=