java - XML解析错误: not well-formed for Twilio

标签 java xml twilio

我一生都无法弄清楚为什么我会收到 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();
}

最佳答案

在进行了一些浏览器调试后,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);
}

关于java - XML解析错误: not well-formed for Twilio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27694878/

相关文章:

java - 如何使用 Java 从 Kafka 中删除 ACLS

java - 在 JFreechart 中使用多个渲染器

java - 如何让一个线程等待通知指定的时间,并根据是否收到通知来执行代码?

java - 严重 : Error configuring application listener of class org. apache.struts2.tiles.Struts Tiles Listener java.lang.NoClassDefFoundError

ruby-on-rails - REXML::Document.new 无法使用 US-ASCII 编码的 XML 进行解析

java - 如何在tomcat web应用程序中动态添加html/jsp页面(开发环境)

c# - XDocument 通过其名称属性的值获取 XML 元素

Twilio:TwiML <Hangup> 动词不结束通话

python - 通过 Twilio 以短信形式发送 MySql 数据

android - 如何在带有 Java 系统属性的 socks 代理后面使用 Twilio Android SDK