I'm trying to parse xml using SaxParser which is some times failing to parse url's that is coming in xml feed. I've xml feed some thing like this.
<songs><song id="269611"><start>2015-01-02T04:36:52Z</start><title>Beyond Me</title><artist>tobyMac</artist><logo>http://ift.tt/1rJrn2j id="77476"><start>2015-01-2T04:32:51Z</start><title>WHOLLY YOURS</title><artist>David Crowder Band</artist><logo>http://ift.tt/1rJrn2l;
When i try to parse the above, some times the url in the logo tag is getting truncated. Instead of http://ift.tt/1rJrlYj this url, i'm just getting Band.jpeg or wder_Band.jpeg etc and some times it gives complete url which is fine. This issue is happening randomly and looks weird. Somebody pls help me on this.
here is the code that i've wrote to parse the above xml:
public class QuuAsyncTask extends AsyncTask<Object, Void, BaseQuuVO> {
private Exception exception;
private QuuServiceDelegate serviceDelegate;
protected BaseQuuVO doInBackground(Object... param) {
BaseQuuVO baseVO = new BaseQuuVO();
try {
QuuDefaultHandler defaultHandler = (QuuDefaultHandler) param[1];
serviceDelegate = (QuuServiceDelegate) param[2];
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL((String) param[0]);
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
xr.setContentHandler(defaultHandler);
InputSource inputSource = new InputSource(sourceUrl.openStream());
inputSource.setEncoding("ISO-8859-1");
xr.parse(inputSource);
baseVO = (BaseQuuVO) defaultHandler.getData();
return baseVO;
} catch (Exception e) {
this.exception = e;
return null;
}
}
protected void onPostExecute(BaseQuuVO result) {
super.onPostExecute(result);
// TODO: check this.exception
// TODO: do something with the feed
if (result != null) {
serviceDelegate.quuResponseReceived(result);
} else {
serviceDelegate.quuResponseFailure("Problem loading data");
}
}
public class PlayListXMLParser extends QuuDefaultHandler {
private List<PlayListSongVO> songsVO;
private String tempVal;
private PlayListSongVO tempPlayListSongVO;
private PlayListsVO playListsVO = new PlayListsVO();
public PlayListXMLParser() {
songsVO = new ArrayList<PlayListSongVO>();
}
@Override
public PlayListsVO getData() {
playListsVO.setSongs(songsVO);
return playListsVO;
}
// Event Handlers
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// reset
tempVal = "";
if (qName.equalsIgnoreCase("song")) {
// create a new instance of employee
tempPlayListSongVO = new PlayListSongVO();
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
tempVal = new String(ch, start, length);
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase("song")) {
// add it to the list
songsVO.add(tempPlayListSongVO);
} else if (qName.equalsIgnoreCase("id")) {
tempPlayListSongVO.setId(tempVal);
} else if (qName.equalsIgnoreCase("start")) {
tempPlayListSongVO.setStart(tempVal);
} else if (qName.equalsIgnoreCase("title")) {
tempPlayListSongVO.setTitle(tempVal);
} else if (qName.equalsIgnoreCase("artist")) {
tempPlayListSongVO.setArtist(tempVal);
} else if (qName.equalsIgnoreCase("logo")) {
tempPlayListSongVO.setLogo(tempVal);
} else if (qName.equalsIgnoreCase("lyrics")) {
tempPlayListSongVO.setLyrics(tempVal);
} else if (qName.equalsIgnoreCase("bio")) {
tempPlayListSongVO.setBio(tempVal);
} else if (qName.equalsIgnoreCase("coupon")) {
tempPlayListSongVO.setCoupon(tempVal);
} else if (qName.equalsIgnoreCase("ad")) {
tempPlayListSongVO.setAd(tempVal);
}
}
}
No comments:
Post a Comment