Java GWT在服务器上检索XML数据
我尝试从外部服务器检索XML数据,并在GWT应用程序上显示信息 问题是我没有成功地显示xml信息 我的Java代码:Java GWT在服务器上检索XML数据,java,xml,gwt,Java,Xml,Gwt,我尝试从外部服务器检索XML数据,并在GWT应用程序上显示信息 问题是我没有成功地显示xml信息 我的Java代码: protected void retrieveXmlDataTop(final VerticalPanel c) { url = "http://www.myapifilms.com/imdb/top?format=XML&start=1&end=25&data=S"; RequestBuilder builder = new Requ
protected void retrieveXmlDataTop(final VerticalPanel c) {
url = "http://www.myapifilms.com/imdb/top?format=XML&start=1&end=25&data=S";
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,URL.encode(url));
try
{
builder.sendRequest(null, new RequestCallback() {
@Override
public void onError(Request request, Throwable exception) {
// Couldn't connect to server (could be timeout, SOP
// violation, etc.)
Window.alert("Couldn't retrieve XML");
}
@Override
public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode())
{
// Process the response in response.getText()
parseMessage(c , response.getText());
}
else
{
// Handle the error. Can get the status text from
// response.getStatusText()
//Window.alert("Error: " + response.getStatusText());
Label error = new Label("Error");
c.add(error);
}
}
});
}
catch (RequestException e)
{
// Couldn't connect to server
Window.alert("Couldn't connect to server to retrieve the XML: \n");
}
}
private void parseMessage(final VerticalPanel c, String messageXml)
{
try
{
// parse the XML document into a DOM
Document messageDom = XMLParser.parse(messageXml);
String tag1Value = messageDom.getElementsByTagName("ranking").item(0)
.getFirstChild().getNodeValue();
String tag2Value = messageDom.getElementsByTagName("title").item(0)
.getFirstChild().getNodeValue();
//Window.alert("Ranking Value: " + tag1Value + "\n" + "Title Value: " + tag2Value);
Label result = new Label("Ranking Value: " + tag1Value + "\n" + "Title Value: " + tag2Value);
c.add(result);
}
catch (DOMException e)
{
Window.alert("Could not parse XML document.");
}
}
和我的xml页面:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<movies>
<movie>
<idIMDB>tt0111161</idIMDB>
<ranking>1</ranking>
<rating>9.2</rating>
<title>The Shawshank Redemption</title>
<urlPoster>http://ia.media-imdb.com/images/M/MV5BODU4MjU4NjIwNl5BMl5BanBnXkFtZTgwMDU2MjEyMDE@._V1_SX34_CR0,0,34,50_AL_.jpg</urlPoster>
<year>1994</year>
</movie>
</movies>
我总是收到错误消息。由于错误,您无法得到答案。状态代码0表示提示为 如果打开浏览器的开发工具,您可能会看到请求和响应,并且可能是关于请求安全问题的警告
如果您想从服务器上获得答案,您的前端必须与API或API服务器来自同一来源,并且浏览器需要兼容。您从服务器接收到哪些状态代码?我想接收XML代码的所有信息是的,我理解,但您收到的实际状态代码是什么,也就是说,response.getStatusCode返回什么?response.getStatusCpde=0,如果我将0放在200的位置,我将输入条件,但在得到Windows警报后:Window.alerts无法解析XML文档。;在my function parseMessage.or中,API需要与CORS兼容-浏览器也需要如此,如果浏览器不支持CORS,请求仍然不会发送。当然,只需要确保您不是在“企业”或内部应用程序上工作,而您不能使用真正的浏览器;。我真的很喜欢在企业软件方面工作。要解决的问题比真正的浏览器还多;