0
我正在嘗試將Usurv調查集成到我的網站。爲此,我需要使用HTTP POST向URL http://app.usurv.com/API/Gateway.svc/getcampaignforframe提交XML請求。然後,響應應包含指向調查的唯一網址。使用Java提交XML發佈請求
可惜我不能得到它的工作 - 代碼編譯正確,但是當我加載網頁,我得到以下異常:
"WARNING: URL = http://app.usurv.com/API/Gateway.svc/getcampaignforframe
[Fatal Error] CampaignFrameRequest%3E:6:3: The element type "link" must be terminated by the matching end-tag "</link>"."
我真的很困惑,作爲XML不即使有鏈接 標籤,所以我不確定錯誤可能來自哪裏。有沒有人有任何想法可能導致這種情況,以及我如何解決它?
下面是Java代碼:
public class UsurvSurveyElement extends RenderController
{
private static Logger LOG = Logger.getLogger(UsurvSurveyElement.class.getName());
String xml = "<CampaignFrameRequest xmlns='http://Qsurv/api' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'><PartnerId>236</PartnerId><PartnerWebsiteID>45</PartnerWebsiteID><RespondentID>1</RespondentID><RedirectUrlComplete>http://localhost:8080/eveningstar/home</RedirectUrlComplete><RedirectUrlSkip>http://localhost:8080/eveningstar/home</RedirectUrlSkip></CampaignFrameRequest>";
String strURL = "http://app.usurv.com/API/Gateway.svc/getcampaignforframe";
@Override
public void populateModelBeforeCacheKey(RenderRequest renderRequest, TopModel topModel, ControllerContext controllerContext)
{
super.populateModelBeforeCacheKey(renderRequest, topModel, controllerContext);
PostMethod post = new PostMethod(strURL);
try
{
// Specify content type and encoding
// If content encoding is not explicitly specified
// ISO-8859-1 is assumed
post.setRequestHeader(
"Content-type", "text/xml; charset=ISO-8859-1");
LOG.warning("request headers: " +post.getRequestHeader("Content-type"));
StringRequestEntity requestEntity = new StringRequestEntity(xml);
post.setRequestEntity(requestEntity);
LOG.warning("request entity: " +post.getRequestEntity());
String response = post.getResponseBodyAsString();
LOG.warning("XML string = " + xml);
LOG.warning("URL = " + strURL);
topModel.getLocal().setAttribute("thexmlresponse",response);
}
catch(Exception e)
{
LOG.warning("Errors while executing postMethod "+ e);
}
try
{
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document document = docBuilder.parse(strURL+xml);
processNode(document.getDocumentElement());
LOG.warning("doc output = " + document);
}
catch(Exception e)
{
LOG.warning("Errors while parsing XML: "+ e);
}
}
private void processNode(Node node) {
// do something with the current node instead of System.out
LOG.warning(node.getNodeName());
NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node currentNode = nodeList.item(i);
if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
//calls this method for all the children which is Element
LOG.warning("current node: " + currentNode);
processNode(currentNode);
}
}
}
}
感謝您的。不幸的是,我現在得到一個空指針異常。響應變量爲空,我不知道爲什麼它沒有選擇服務器響應(應該是一個URL)。 – Victoria