I am facing issue while posting image in server. I am using httpConnection, and am getting issues such as Connection closed, Not Connected, TCP Timeout, Null Exception. Wwhile posting, I am posting a JSON Object,These issues occurs inconsistently, but happens randomly, I am not able to understand rootcause of issue?
byte[] buffer = new byte[20000];
int bytesRead = responseData.read(buffer);
while (bytesRead > 0) {
byteArray.write(buffer, 0, bytesRead);
bytesRead = responseData.read(buffer);
}
byteArray.close();
ow i replaced to
byte[] byteArray = IOUtilities.streamToBytes(request.openInputStream());
I am trying to understand whether these change will be useful?
I am posting sample piece of Code for posting pic
request = (HttpConnection) connect.getConnection();
request.setRequestMethod("POST");
request.setRequestProperty("Content-Type", "application/json");
os = request.openOutputStream();
JSONObject jsonObjSMS;
Vector dataValue = new Vector();//Vector for putting Images
try {
jsonObjSMS = new JSONObject();
jsonObjSMS.put("strNo", MyObject.getNumber());
byte[] encoded = Base64OutputStream.encode(dataValues, 0,dataValues.length,false, false);
String baseEncodedStringArrayValue = new String(encoded, "UTF-8");
dataValue.addElement(baseEncodedStringArrayValue);
jsonObjSMS.put("imageArray", dataValue);
}catch(Exception e){
System.out.println("Exception in JSON"+e);
}
os.write(jsonObjSMS.toString().getBytes("UTF-8"));
os.flush();
byte[] byteArray = IOUtilities.streamToBytes(request.openInputStream());
jsonResponse = new String(byteArray, "UTF-8");
Am I missing any piece of Code which is creating this error randomly?