我正在为需要发送短信的学校创建一个 android 应用程序。我已经有几个人推荐了 twilio,所以我决定接受它。遵循本教程:https://www.twilio.com/blog/2016/05/how-to-send-an-sms-from-android.html,使用我自己的 android 应用程序和包括我在内的应用程序得到 404当 post 被调用时(通过 ngrok 看到)。我完全不知所措,对android还是很陌生。后端通过 intellij 给出以下消息: 8810 [qtp649044888-15] INFO spark.http.matching.MatcherFilter - The requested route [/] has not been mapped in Spark for Accept: [null]
账户ID、地址、电话号码等替换为x的Backend:
//Heroku assigns different port each time, hence reading it from process.
ProcessBuilder process = new ProcessBuilder();
Integer port;
if (process.environment().get("PORT") != null) {
port = Integer.parseInt(process.environment().get("PORT"));
} else {
port = 4567;
}
Spark.port(port);
get("/", (req, res) -> "Hello, World");
TwilioRestClient client = new TwilioRestClient.Builder(System.getenv("ACxxxxxxx72279fb226f0dd162c869ce1e"), System.getenv("xxxxxxxc324a89155b93b5af26393297")).build();
post("/sms", (req, res) -> {
String body = req.queryParams("Body");
String to = req.queryParams("To");
String from = System.getenv("216450xxxx");
Message message = new MessageCreator(
new PhoneNumber(to),
new PhoneNumber(from),
body).create(client);
return message.getSid();
});
应用程序发布方法
Call post(String url, Callback callback) throws IOException {
RequestBody formBody = new FormBody.Builder().add("To", "609420xxxx").add("Body", "TEST").build();
Request request = new Request.Builder().url(url).post(formBody).build();
Call response = mClient.newCall(request);
response.enqueue(callback);
Log.d("CALL: ", response.toString()+ " " + callback.toString());
return response;
}
按钮上的应用发布方法:
emailButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
post("http://xxxxxxxx.ngrok.io", new Callback(){
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),"SMS Sent!",Toast.LENGTH_SHORT).show();
}
});
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
});
这应该向我的号码发送文本(在 twilio 帐户上验证),但我什么也没得到。吐司确实显示文本已发送。