httpConnection/HttpConnectionFactory returning garbage while reading xml from server

lord1700

New Member
I am reading xml from server using the following code but it frequently returns garbage instead of xml\[code\]private boolean getNewsWithSAX(){ HttpConnectionFactory connectionFactory = new HttpConnectionFactory("http://iphone-rss.gazeta.ru/iphone-xml/news/lenta.xml"); HttpConnection connection; try { connection = connectionFactory.getNextConnection(); } catch (NoMoreTransportsException e) { System.out.println("ERROR WHILE GETTING CONNECTION : \n"+ e); e.printStackTrace(); return false; } String respond = null; int respondCode = 0; try { respond = connection.getResponseMessage(); respondCode = connection.getResponseCode(); } catch (IOException e) { System.out.println("ERROR WHILE GETTING RESPONSE : \n"+ e); e.printStackTrace(); } System.out.println("GOT RESPONSE >"); System.out.println("respond : " + respond + " | " + "respondCode : " + respondCode); InputStream in = null; try { in = connection.openInputStream(); } catch (IOException e) { System.out.println("ERROR WHILE GETTING STREAM : \n"+ e); e.printStackTrace(); } saveFile(in); return true;}static void saveFile(InputStream in){ System.out.println("SAVEFILE : START"); String path = "file:///SDCard/BlackBerry/documents/"; Calendar cal = Calendar.getInstance(); FileConnection fileCon = null; try { fileCon = (FileConnection) Connector.open(path+ cal.get(Calendar.DAY_OF_MONTH)+"_"+cal.get(Calendar.MONTH)+"_"+cal.get(Calendar.YEAR)+"__"+cal.get(Calendar.HOUR_OF_DAY)+"_"+cal.get(Calendar.MINUTE)+"_"+cal.get(Calendar.SECOND)+".xml"); } catch (IOException e) { e.printStackTrace(); } try { fileCon.create(); } catch (IOException e) { e.printStackTrace(); } OutputStream outStream = null; try { outStream = fileCon.openOutputStream(); } catch (IOException e) { e.printStackTrace(); } byte[] b = new byte[1024]; int len = 0; try { while(-1!=(len=in.read(b))){ System.out.println("BYTE : " + new String(b)); outStream.write(b,0,len); } } catch (IOException e) { e.printStackTrace(); } try { outStream.close(); fileCon.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("SAVEFILE : END");}\[/code\]I am developing for SDK 5.0 and upwardsthe files are saved in the sdcardso when it returns correctly it gives the following contenthttp://pastebin.com/Ypcwe8m6which is finebut it frequently returns garbage values instead of xmlhttp://pastebin.com/TWPnS1VDPreviously i was parsing this XML files but was facing org.xml.sax.SAXParseException: Invalid character '&#x1f' encountered.so decided to first just fetch the xml and thats what this code does.I have checked implementing the same thing on android it works flawlessly it seems problem with Blackberry api it selfCan anybody please draw me towards solution.
 
Back
Top