I am trying to use XMLDecoder to deserialize an XML string but it is throwing a java.lang.ArrayIndexOutOfBoundsException.
It seems that the decoder is throwing the exception because if the stream contains no objects (or no more objects) it throws the ArrayIndexOutOfBoundsException but when I print the string it has the correct XML.
The response that it is trying to deserialize is :
<response>
<isMaster>true</isMaster>
<success>true</success>
<sessionsinfo>
<sessioninfo>
<sessionID>2018101517193766902543106540502401</sessionID>
<contactID>9134049637760000241</contactID>
<startTime>2018-10-15T17:19:37.662-05:00</startTime>
<stopTime>2018-10-15T17:20:32.011-05:00</stopTime>
<primaryinum>405001000000207</primaryinum>
<contenttypeofprimaryinum>Audio</contenttypeofprimaryinum>
<recordedmedia>
<Audio/>
</recordedmedia>
</sessioninfo>
</sessionsinfo>
</response>
public static SessionInfoDevice GetQuerySessionByDevice(String ip, String extension)
{
String surl = ip + "/servlet/eQC6?&responseType=XML&interface=ISessionManagement&method=querysessioninfobydevice&device.device=" + extension;
URL url;
try {
url = new URL(surl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setRequestProperty("Accept", "application/json");
if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
//XMLDecoder dec = new XMLDecoder(conn.getInputStream());
//Object ob = dec.readObject();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line=null;
int i=0;
while((line = br.readLine()) != null) {
if (i == 0 || i==6 || i==7) {
}else {
System.out.println(i);
sb.append(line + '\n');
}
i++;
}
br.close();
System.out.println("" + sb.toString());
//XMLDecoder decoder = new XMLDecoder(org.apache.commons.io.IOUtils.toInputStream(sb.toString()));
XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(new ByteArrayInputStream (sb.toString().getBytes())));
//SessionInfoDevice sid = (SessionInfoDevice) decoder.readObject();
Object o = decoder.readObject();
decoder.close();
return null;
}else {
System.out.println(conn.getResponseCode());
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
//return null;
}