10-30-2009 07:58 PM
I've seen posts on issues with retrieving XML and retrieving gzip data. Seperately, i have no problem doing either. But i am unable to send a gzipinputstream to my xml parser - it just throws exception and says no stack trace.
I send a parameter in my http request that determines if the server should return an XML file or the gziped XML file. when NOT requesting gzip, i can read and parse fine. if i request gzip and just read steam like this ...
byte[] data = new byte[256];
int len = 0;
int size = 0;
StringBuffer raw = new StringBuffer();
while ( -1 != (len = inputStream.read(data)) )
{
raw.append(new String(data, 0, len));
size += len;
}it prints out just like my xml ... so i would assume it means its able to recieve the gzip data fine.
However, if i request gzip and try to parse using the same code that works when i just parse an xml, it fails. Is there another step i need to do to convert the inputstream so the xml handler can read it?
httpConnection = (HttpConnection)Connector.open(url);
inputStream = httpConnection.openDataInputStream();
if(httpConnection.getResponseCode() == HttpConnection.HTTP_OK)
{
inputStream = new GZIPInputStream(inputStream);
InputSource is = new InputSource(inputStream);
is.setEncoding(desiredEncoding);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
DefaultHandler hcHandler = new XMLParse();
parser.parse(is,hcHandler);
} //end if(httpConnection.getResponseCode() == HttpConnection.HTTP_OK)
Solved! Go to Solution.
10-30-2009 08:13 PM
you should check to see if the content is gzip:
String encoding = m_httpConnection.getHeaderField("Content-Encoding"
if ((encoding != null) && (encoding.indexOf("gzip") != -1)){
compressed = true;
}
Then, unzip it:
if (compressed){
GZIPInputStream gzip = new GZIPInputStream(dataInputStream);
DataInputStream tmp = new DataInputStream(gzip);
dataInputStream = tmp;
}
Now you can safely parse the XML.
10-31-2009 01:51 PM
yup this works, thanks. unfortunately it appears gzip doesnt seem to solve my problem ... it appears it may be my xml parser thats taking a long time and not the http transfer. or more likely im just doing somethig else wrong.
11-04-2009 08:52 AM
how to parse an xml in BB i m wondering to do xml parser in strom device
11-04-2009 08:53 AM
can anyone help me to do xml parser using BB rim api's
11-04-2009 09:18 AM
@bala_eventurers: have you looked at the xmldemo? i just used that as a template for mine.