Reputation: 75
I am trying to access data from a raw data stream. Before accessing the raw data stream, I must parse/remove any traces of xml. I do this by converting the raw stream to a string array and by iterating through each line to remove the xml.
I can parse/remove the xml from the string okay and I can recreate the data stream. The problem I have is reading through each line and storing each value (year, month, day, power) The while loop fails with an out of bound exception on the line: int year = bb.get(0);
My guess is that this is happening because the newResult string that's created isn't being read correctly through the readLine method. Instead of having a list of raw data, there's only one line of raw data. According to the specifications, there should be 60 (and the length of decResult-1 is 60.
Here is the code below. Can anyone help me to figure out why the while loop isn't iterating through each line? Thanks.
try {
HttpRequestBase request = null;
request = new HttpGet(urlString);
consumer.sign(request);
Log.v(TAG, "consumer.sign");
HttpResponse response = client.execute(request);
InputStream stream = (InputStream)response.getEntity().getContent();
String result = convertStreamToString(stream);
String[] decResult = result.split("<day raw=\"");
String newResult = "";
if(decResult.length > 1) {
for(int i=1; i<decResult.length; i++) {
decResult[i] = decResult[i].replaceAll("\"\\/>", "");
Log.v(TAG, "DecResult = "+ (decResult[i]).toString());
decResult[i] = decResult[i].replaceAll("</ted5000_mtu_days>","");
Log.v("DayData", decResult[i].toString());
newResult = newResult.concat((decResult[i].toString() + "\n"));
}
}
Log.v("DayData", "newResult = "+ newResult);
Log.v("DayData", "End NewResult");
BufferedReader reader = new BufferedReader(new StringReader(newResult));
String line = null;
while((line = reader.readLine()) != null) {
Log.v("DayData", "****Entering while loop***");
Log.v("DayData", "Line = " + line);
String bytes = Base64.decode(line);
Log.v("DayData", "Received bytes");
ByteBuffer bb = ByteBuffer.wrap(bytes.getBytes());
Log.v("DayData", "ByteBuffer.wrap");
bb.order(ByteOrder.LITTLE_ENDIAN);
int year = bb.get(0);
int month = bb.get(1);
int day = bb.get(2);
int power = bb.getInt(3);
Log.i("DayData", "Date: " + month + "/" + day + "/" + year + " Power: " + power);
}
Log.v("DayData", "Exiting while loop");
} catch (Exception e) {
e.printStackTrace();
Log.e("DayData", "Exception: " + e.toString());
return false;
}
}
Upvotes: 1
Views: 1044
Reputation: 236114
You should use an XML parser that suits your needs. From the wikipedia page:
Existing APIs for XML processing tend to fall into these categories:
Stream-oriented APIs accessible from a programming language, for example SAX and StAX.
Tree-traversal APIs accessible from a programming language, for example DOM.
XML data binding, which provides an automated translation between an XML document and programming-language objects.
Declarative transformation languages such as XSLT and XQuery.
Upvotes: 1
Reputation: 12883
It sounds like you're bombing out on an empty line. Since you already have lots of debug logging you could verify that by changing this line Log.v("DayData", "Received bytes"); to something like this Log.v("DayData", "Received bytes: \""+bytes+"\"");
You probably should still check for this condition, even if you find some other error is causing the problem. If you expect a certain length, you can check that the .limit() on the buffer meets your expectations.
Upvotes: 1