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?开发者_StackOverflow中文版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.
You are getting this error because.....
"This exception is thrown when a program attempts to create an URL from an
incorrect specification."
before passing the string to url you can Convert the string to Standard URL format
this way..
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"));
You can use this function anywhere you are fetching URL and using it, so you can overcome the Error and make your string work.
i know you accepted the answer but this could also help other also i think Thank You.
"http://w3devadv.liveproj.com /api/apiRequest.php?Method=getdealdetails&DealId=2&SessionId=EA3JQ0RZJT4e66223143fc5"
It is probably due to the space in the URL.
精彩评论