我正在尝试使用 YOURLS 创建自定义 URL 缩短器。当我在 YOURLS 中使用内置 API 时,这就是 chrome 作为 XML 文档出现的内容:
<result>
<url>
<keyword>2</keyword>
<url>http://www.bing.com</url>
<title>http://www.bing.com</title>
<date>2013-06-08 19:24:28</date>
<ip>127.0.0.1</ip>
</url>
<status>success</status>
<message>http://www.bing.com added to database</message>
<title>http://www.bing.com</title>
<shorturl>http://127.0.0.1/2</shorturl>
<statusCode>200</statusCode>
</result>
我只想要“shorturl”中的东西
但是,我在 eclipse 中遇到错误
URL url=null;
try {
url = new URL("http://localhost/yourls/yourls-api.php?username=username&password=password&action=shorturl&url="+"http://google.ca");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream()));
String s = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = (Document) db.parse((url).openStream());
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = (XPathExpression) xpath.compile("shorturl");
Object exprResult = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodeList = (NodeList) exprResult;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
在
Object exprResult = expr.evaluate(doc, XPathConstants.NODESET);
错误提示“XPathExpressions 类型中的方法评估(节点,短,对象)不适用于参数(文档,QName)”
有人知道如何解决这个问题吗?