簡體   English   中英

XML解析錯誤:Twilio格式不正確

[英]XML Parsing Error: not well-formed for 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)中提供了一些實用程序功能,用於處理轉義空格等。

這是生成操作網址的方法的新實現:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM