From 5c14315b4abb955bb9b08a68c7051587284224d2 Mon Sep 17 00:00:00 2001 From: Rushdi Date: Sat, 16 Apr 2022 00:24:53 +0530 Subject: [PATCH] Add JSONException --- .../traccar/api/service/addons/TraccarClient.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClient.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClient.java index 65ca668e8c..5853edbe72 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClient.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClient.java @@ -27,6 +27,7 @@ import okhttp3.RequestBody; import okhttp3.Response; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.json.JSONException; import org.json.JSONObject; import org.wso2.carbon.device.mgt.common.TrackerDeviceInfo; 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 { response = client.newCall(request).execute(); if(method==TraccarHandlerConstants.Methods.POST){ - JSONObject obj = new JSONObject(response.body().string()); - response.close(); - if (obj != null){ + String result = response.body().string(); + JSONObject obj = new JSONObject(result); + if (obj.has("id")){ int traccarId = obj.getInt("id"); try { TrackerManagementDAOFactory.beginTransaction(); @@ -140,6 +141,10 @@ public class TraccarClient implements org.wso2.carbon.device.mgt.core.traccar.ap } } TrackerManagementDAOFactory.commitTransaction(); + } catch (JSONException e) { + TrackerManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred on JSON object ."; + log.error(msg, e); } catch (TransactionManagementException e) { TrackerManagementDAOFactory.rollbackTransaction(); 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(); } } + response.close(); } if (log.isDebugEnabled()) { log.debug("Successfully the request is proceed and communicated with Traccar");