【问题标题】:XML Parsing Error: not well-formed for TwilioXML 解析错误:Twilio 格式不正确
【发布时间】:2014-12-30 04:06:00
【问题描述】:

对于我的一生,我无法弄清楚为什么我会收到 Twilio 响应对象的操作 URL 的 XML 解析错误。我已经附上了输出页面错误和用于生成 XML 的 java 代码。

输出错误

XML Parsing Error: not well-formed
Location: https://test.ignore.com/ApplicationName/go.acx?action=ivr.outbound.twilio.Introduction&rkey=1
Line Number 4, Column 101:<Response><Gather action="instanceurl.com/AccessWorx/go.acx?action=ivr.outbound.twilio.Selection&rkey=1" timeout="5" numDigits="1" finishOnKey="#" method="GET"><Say>This is an automated message from __________ to notify you of a service issue.Here is a sample message. Press 1 to accept this serviceissue, Press 2 to forward this call to the next contact in you company, press 3 if you are not the correct person to contact, press 4 to repeat these options.</Say></Gather></Response>
----------------------------------------------------------------------------------------------------^

^ 与上述代码格式不匹配,但它本质上指向操作 url 末尾的 rkey=1 中的“=”符号。

JAVA 代码

StringBuffer sb = new StringBuffer();
    sb.append("This is an automated message from ___________ to notify you of a service issue.")
            .append(serviceMessage)
            .append("Press 1 to accept this service"
            + "issue, Press 2 to forward this call to the next contact in you company, press 3 "
            + "if you are not the correct person to contact, press 4 to repeat these options.");

// Create a TwiML response and add our friendly message.
TwiMLResponse twiml = new TwiMLResponse();
Say say = new Say(sb.toString());

Gather g = new Gather();
// set url to selection with paramter for rkey
IVRAgent ivrAgent = new IVRAgent();
g.setAction(ivrAgent.buildActionUrl(callBean.getInstanceUrl() + "go.acx?", "ivr.outbound.twilio.Selection", rkey.toString()));
g.setTimeout(TIMEOUT);
g.setNumDigits(1);
g.setFinishOnKey(POUND);
g.setMethod("GET");

try {
    g.append(say);
    twiml.append(g);    
} catch (TwiMLException e) {
    log.error("Error in creating twiml", e);
    e.printStackTrace();
}

【问题讨论】:

  • 查看stackoverflow.com/questions/1328538/…。尝试转义 &amp; char。
  • 谢谢 Bhesh ... 原来你是对的!发布问题似乎会立即引导您找到自己的答案。
  • 是的。如果阅读链接中的答案,您也可以从那里的海报中看到类似的体验。 :)

标签: java xml twilio


【解决方案1】:

在做了一些浏览器调试后,Firefox 告诉我 & 需要转义。幸运的是,Java 在 java.net (URLEncoder) 中提供了一些实用函数来处理转义空格、& 等。

这是我生成动作 url 的方法的新实现:

public String buildActionUrl(String instanceUrl, String action, String rkey) {
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("action", action));
    params.add(new BasicNameValuePair("rkey", rkey));

    String paramString = URLEncodedUtils.format(params, "UTF-8");
    try {
        return URLEncoder.encode(instanceUrl + paramString, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        logger.error("", e);
        return ".";     // return the action to be defaulted to the originating page
    }
}

可以推广此方法以接受任何参数的输入映射:

public String buildActionUrl(String baseUrl, Map<String, String> parameters, String encoding) throws UnsupportedEncodingException {
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    for (String param : parameters.keySet() ) {
        params.add(new BasicNameValuePair(param, parameters.get(param)));
    }
    return URLEncoder.encode(baseUrl + URLEncodedUtils.format(params, encoding), encoding);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多