Add JSONException

feature/traccar-sync
Rushdi Mohamed 3 years ago
parent 1a7442ba3d
commit 5c14315b4a

@ -27,6 +27,7 @@ import okhttp3.RequestBody;
import okhttp3.Response; import okhttp3.Response;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.wso2.carbon.device.mgt.common.TrackerDeviceInfo; import org.wso2.carbon.device.mgt.common.TrackerDeviceInfo;
import org.wso2.carbon.device.mgt.common.TrackerGroupInfo; import org.wso2.carbon.device.mgt.common.TrackerGroupInfo;
@ -120,9 +121,9 @@ public class TraccarClient implements org.wso2.carbon.device.mgt.core.traccar.ap
try { try {
response = client.newCall(request).execute(); response = client.newCall(request).execute();
if(method==TraccarHandlerConstants.Methods.POST){ if(method==TraccarHandlerConstants.Methods.POST){
JSONObject obj = new JSONObject(response.body().string()); String result = response.body().string();
response.close(); JSONObject obj = new JSONObject(result);
if (obj != null){ if (obj.has("id")){
int traccarId = obj.getInt("id"); int traccarId = obj.getInt("id");
try { try {
TrackerManagementDAOFactory.beginTransaction(); TrackerManagementDAOFactory.beginTransaction();
@ -140,6 +141,10 @@ public class TraccarClient implements org.wso2.carbon.device.mgt.core.traccar.ap
} }
} }
TrackerManagementDAOFactory.commitTransaction(); TrackerManagementDAOFactory.commitTransaction();
} catch (JSONException e) {
TrackerManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred on JSON object .";
log.error(msg, e);
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
TrackerManagementDAOFactory.rollbackTransaction(); TrackerManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred establishing the DB connection ."; String msg = "Error occurred establishing the DB connection .";
@ -157,6 +162,7 @@ public class TraccarClient implements org.wso2.carbon.device.mgt.core.traccar.ap
TrackerManagementDAOFactory.closeConnection(); TrackerManagementDAOFactory.closeConnection();
} }
} }
response.close();
} }
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Successfully the request is proceed and communicated with Traccar"); log.debug("Successfully the request is proceed and communicated with Traccar");

Loading…
Cancel
Save