【问题标题】:Why am I getting error: A 'From' phone number is required为什么我收到错误消息:需要“发件人”电话号码
【发布时间】:2012-07-25 17:10:46
【问题描述】:

我正在使用 Twilio API 从我的 Twilio 号码向我的手机发送短信。

我有两个文件: 1.一个带有表单和方法调用的JSP文件 2. 一个带有 Twilio API 的 java 类,它接收来自项目 1 的方法调用并执行发送短信部分 (参见下面的文件)

为什么我会收到例外“发件人”电话号码是必需的?

该程序工作正常,交付正确,尝试了很多次。我没有收到错误。有用。只是这个例外让我感到困惑。我已经购买了 twilio 号码,所以我没有使用试用号码。

有一个与之关联的堆栈跟踪,但可以稍后发布。

在 smsParams.put("From", from) 我尝试直接用这样的单元格号码替换 from (使用有效的否): smsParams.put("发件人", "1231231234"); 然后我得到一个异常,它转到下一行 (smsParams.put("To", from);) 并说需要一个 "To" 号码。当我用数字替换变量时,它会跳到下一行并为消息正文提供异常。

谢谢

//***********************************
//java class MyMessage
//***********************************

package package_sms;
import java.util.HashMap;
import java.util.Map;
import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.TwilioRestException;
import com.twilio.sdk.resource.factory.SmsFactory;
import com.twilio.sdk.resource.instance.Account;


public class MyMessage {

/** The Constant ACCOUNT_SID. */
public static final String ACCOUNT_SID = "AC11d68faa7db85a48557aa33ae0b88261";

/** The Constant AUTH_TOKEN. */
public static final String AUTH_TOKEN = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";


public String to, from, text;

    public void provideNumbers(String to, String from, String text)
    {
        // Create a rest client
        TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

        // Get the main account (The one we used to authenticate the client
        Account mainAccount = client.getAccount();

        // Send an sms
        SmsFactory smsFactory = mainAccount.getSmsFactory();
            Map<String, String> smsParams = new HashMap<String, String>();
        smsParams.put("From", from);    // Twillio No
        smsParams.put("To", to);        // target phone No
        smsParams.put("Body", text);    // message text
        try 
        {
            smsFactory.create(smsParams);
        } 
        catch (TwilioRestException e) 
        {
            e.printStackTrace();
        }   
    }



<% *****************************
// jsp file SendSms
***************************** %>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="package_sms.MyMessage"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>form</title>
</head>
    <body style="background-color:Azure;">
        <h3>Welcome to your text messenger</h3>
        <form action="SendSms.jsp" name="form1" method="POST" >
        From: <input type="text" name="phoneFrom" style="background-color: Beige;border:1px solid Black;"/><br><br> 
        To: <input type="text" name="phoneTo" style="background-color: Beige;border:1px solid Black;"/><br><br>
        Message text: <input type="text" name="messageText" style="background-color: Beige;border:1px solid Black;"/><br><br>
        <input type="submit" name="submit" value="Send Message" />
        </form><br>

        <%  /* Declared string variables that provide parameters to the 
               call to provideNumber method. provideNumbers method is located in MyMessage class.
               provideNumbers method is called on a 'message' object.  */

        String number_from = request.getParameter("phoneFrom");
        String number_to = request.getParameter("phoneTo");
        String messageBody = request.getParameter("messageText");

        MyMessage message = new MyMessage();
            message.provideNumbers(number_to, number_from, messageBody);
            %>

    </body>
</html>

【问题讨论】:

  • 1) 您分享了没有要查看或更正的代码。很难以这种方式提供帮助。 2) 阅读 API 文档,也许该方法要求这些输入是强制性的。
  • 对不起。我刚刚添加了代码。 API 文档确实需要这些值,但我确实提供了它们——通过 from、to 和类文件中的文本参数(例如 smsParams.put("From", from);)。
  • 您能否在发送消息之前立即记录“发件人”值并查看它是什么?

标签: jsp twilio


【解决方案1】:

错误出现在您的 JSP 页面中。加载 JSP 时,它会执行所有代码。该代码的一部分是您获取输入并调用您的方法。

但是,当文件首次加载时,表单中没有值。因此 getParameter 调用得到空值。因此,“message.provideNumbers”方法正在传递您的 Java 代码空值。这反过来意味着当您的 Java 代码尝试调用 Twilio 时,它会传递空值并触发异常。

要更正此问题,您可以将 JSP Java 代码包含在 IF 语句中,检查变量是否具有条目。例如:

<% if (request.getParameter("phoneFrom") != null 
       && request.getParameter("phoneTo") != null 
       && request.getParameter("messageText") != null) {  

    String number_from = request.getParameter("phoneFrom");
    String number_to = request.getParameter("phoneTo");
    String messageBody = request.getParameter("messageText");

    MyMessage message = new MyMessage();
    message.provideNumbers(number_to, number_from, messageBody);
}%>

希望对您有所帮助。

【讨论】:

  • 完全有帮助。异常消失了。你写的很有意义。谢谢理查德。另外还要感谢 Alfabravo 的意见。
【解决方案2】:

尽管您在 JSP 中包含了 MyMessage,但您并没有为 from、to 和 message 变量设置值。 从请求中获取它们并初始化这些变量。

我刚刚看到我确实监督了一些事情......在您的 JSP 末尾,有来自请求的分配和 JSP 上的一些逻辑。我建议将包含的类中的请求作为 bean 处理,因此您确定该过程在“绘制”页面之前完成,因此您将能够显示一些处理结果。

在您的 JSP 中,一开始

<jsp:useBean id="myBean" scope="request" class="package_sms.MyMessage">
   <% myBean.provideNumbers( request ); %>
</jsp:useBean>

...在你的课堂上,类似这样的事情

public void provideNumbers(HTTPServletRequest request)
{
    String number_from = request.getParameter("phoneFrom");
    String number_to = request.getParameter("phoneTo");
    String messageBody = request.getParameter("messageText");
    ...
    smsParams.put("From", number_from);    // Twillio No
    smsParams.put("To", number_to);        // target phone No
    smsParams.put("Body", messageBody);    // message text

在类中,您可以设置Public String resultString,并根据发送过程的结果显示一条消息,并且在JSP中,您可以使用&lt;%= myBean.resultString %&gt;之类的内容打印出来

【讨论】:

  • 感谢您的建议。不过我很困惑。您在这些调用中指的是“From”、“To”和“Body”:'smsParams.put("From", from)、smsParams.put("To", to) 和 smsParams.put("Body" , 文本) ?所以你的意思是 smsParams.put 调用中的第一个参数,而不是第二个,对吗?我想我提供了第二个参数。你说我不是。感谢您澄清。谢谢。
  • 已编辑答案。或许能帮上忙
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-15
  • 1970-01-01
  • 2015-01-25
  • 1970-01-01
  • 2017-04-30
  • 1970-01-01
相关资源
最近更新 更多