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