use safeGetString

This commit is contained in:
Milos Kozak 2018-03-12 11:22:19 +01:00
parent 601dbc1036
commit fc070c95a7
3 changed files with 15 additions and 4 deletions

View file

@ -63,6 +63,7 @@ import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.Overview.notifications.Notification; import info.nightscout.androidaps.plugins.Overview.notifications.Notification;
import info.nightscout.utils.DateUtil; import info.nightscout.utils.DateUtil;
import info.nightscout.utils.FabricPrivacy; import info.nightscout.utils.FabricPrivacy;
import info.nightscout.utils.JsonHelper;
import info.nightscout.utils.SP; import info.nightscout.utils.SP;
import io.socket.client.IO; import io.socket.client.IO;
import io.socket.client.Socket; import io.socket.client.Socket;
@ -351,7 +352,7 @@ public class NSClientService extends Service {
} }
if (Config.detailedLog) if (Config.detailedLog)
try { try {
MainApp.bus().post(new EventNSClientNewLog("ANNOUNCEMENT", data.has("message") ? data.getString("message") : "received")); MainApp.bus().post(new EventNSClientNewLog("ANNOUNCEMENT", JsonHelper.safeGetString(data, "message", "received")));
} catch (Exception e) { } catch (Exception e) {
FabricPrivacy.logException(e); FabricPrivacy.logException(e);
} }
@ -577,9 +578,7 @@ public class NSClientService extends Service {
// remove from upload queue if Ack is failing // remove from upload queue if Ack is failing
UploadQueue.removeID(jsonFood); UploadQueue.removeID(jsonFood);
String action = null; String action = JsonHelper.safeGetString(jsonFood, "action");
if (jsonFood.has("action"))
action = jsonFood.getString("action");
if (action == null) { if (action == null) {
addedFoods.put(jsonFood); addedFoods.put(jsonFood);

View file

@ -27,6 +27,16 @@ public class JsonHelper {
return result; return result;
} }
public static String safeGetString(JSONObject json, String fieldName, String defaultValue) throws JSONException {
String result = defaultValue;
if (json.has(fieldName)) {
result = json.getString(fieldName);
}
return result;
}
public static double safeGetDouble(JSONObject json, String fieldName) throws JSONException { public static double safeGetDouble(JSONObject json, String fieldName) throws JSONException {
double result = 0d; double result = 0d;

View file

@ -19,6 +19,8 @@ public class JsonHelperTest {
JSONObject object = new JSONObject(jsonString); JSONObject object = new JSONObject(jsonString);
assertEquals(null, JsonHelper.safeGetString(object, "notexisting")); assertEquals(null, JsonHelper.safeGetString(object, "notexisting"));
assertEquals("5", JsonHelper.safeGetString(object, "s")); assertEquals("5", JsonHelper.safeGetString(object, "s"));
assertEquals("default", JsonHelper.safeGetString(object, "notexisting", "default"));
assertEquals("5", JsonHelper.safeGetString(object, "s", "default"));
assertEquals(0.0d, JsonHelper.safeGetDouble(object, "notexisting"), 0.0d); assertEquals(0.0d, JsonHelper.safeGetDouble(object, "notexisting"), 0.0d);
assertEquals(3.0d, JsonHelper.safeGetDouble(object, "d"), 0.000001d); assertEquals(3.0d, JsonHelper.safeGetDouble(object, "d"), 0.000001d);