XML解析错误:Twilio格式不正确

时间:2014-12-29 19:34:45

标签: 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>
----------------------------------------------------------------------------------------------------^

^在上面的代码格式中没有匹配,但它基本上指向&#34; =&#34;在动作网址的末尾登录rkey = 1。

JAVA CODE

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();
}

1 个答案:

答案 0 :(得分:2)

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

以下是生成动作网址的方法的新实现:

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);
}