And some times i'm getting java.net.MalFormedURLException what's the reason behind this and how can i resolve this..
My code is as follows..
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL(
"http://w3devadv.liveproj.com /api/apiRequest.php?Method=getdealdetails&DealId=2&SessionId=EA3JQ0RZJT4e66223143fc5");
/**
* Create handler to handle XML Tags ( extends DefaultHandler )
*/
DealsHandler myXMLHandler = new DealsHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
In handler i'm writing the following code in startelement
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentElement = true;
if (localName.equalsIgnoreCase("data")) {
dealsdata = new DealsData();
} else if (localName.equalsIgnoreCase("dealdetails")) {
deals = new Deals();
} else if (localName.equalsIgnoreCase("title")) {
deals.title = attributes.getLocalName(0);
}
i'm getting the above said exception how can i resolve this.
答案 0 :(得分:2)
您收到此错误是因为.....
"This exception is thrown when a program attempts to create an URL from an
incorrect specification."
答案 1 :(得分:1)
在将字符串传递给url之前 您可以将字符串转换为标准网址格式
这样......String url = new String(str.trim().replace(" ", "%20").replace("&", "%26")
.replace(",", "%2c").replace("(", "%28").replace(")", "%29")
.replace("!", "%21").replace("=", "%3D").replace("<", "%3C")
.replace(">", "%3E").replace("#", "%23").replace("$", "%24")
.replace("'", "%27").replace("*", "%2A").replace("-", "%2D")
.replace(".", "%2E").replace("/", "%2F").replace(":", "%3A")
.replace(";", "%3B").replace("?", "%3F").replace("@", "%40")
.replace("[", "%5B").replace("\\", "%5C").replace("]", "%5D")
.replace("_", "%5F").replace("`", "%60").replace("{", "%7B")
.replace("|", "%7C").replace("}", "%7D"));
您可以在获取URL并使用它的任何地方使用此功能,这样您就可以克服错误并使字符串正常工作。
我知道你接受了答案,但这也可以帮助其他人 谢谢。
答案 2 :(得分:0)
"http://w3devadv.liveproj.com /api/apiRequest.php?Method=getdealdetails&DealId=2&SessionId=EA3JQ0RZJT4e66223143fc5"
可能是由于URL中的空格。