您好我正在使用HttpURLConnection发送服务器请求,并且XML输入流响应在多线程环境中由javax.xml.bind.Unmarshaller转换为java对象。它工作正常。但在某些时候,(即)在解组输入流时,线程被阻止了。那时我没有任何例外。
调用convertStreamToObject方法后,线程被阻塞。
请任何人帮我解决问题。代码在
之下 try {
url = new URL(requestURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setConnectTimeout(1500);
connection.setReadTimeout(1500);
try {
inputStream = connection.getInputStream();
} catch (IOException e) {
OPERATION_LOGS.error("IOException in sendRequest()",e);
}if(inputStream != null){
T response = convertStreamToObject(inputStream, responseObjType);
}
} catch(Exception e ) {
OPERATION_LOGS.error("Exception in sendRequest()",e);
}
public <K> K convertStreamToObject(InputStream inputStream, Class<K> type) {
try {
return (K) XMLUtility.parseXML(inputStream, type);
} catch (Exception e) {
OPERATION_LOGS.error(EXCEPTION , e);
}
return null;
}
public static <T> T parseXML(InputStream inputStream, Class<T> type) {
T beanObject = null;
Unmarshaller unmarshObj = null;
try {
JAXBContext context = JAXBContext.newInstance(type);
if (context != null) {
unmarshObj = context.createUnmarshaller();
}
if (unmarshObj != null) {
// Converting the xml into corresponding bean
beanObject = (T) unmarshObj.unmarshal(inputStream);
}
OPERATION_LOGS.debug("Response XML:" + encodeXML(beanObject, type));
} catch (JAXBException e) {
OPERATION_LOGS.error(e.getMessage(),e);
}
return beanObject;
}