use safeGetString
This commit is contained in:
parent
601dbc1036
commit
fc070c95a7
|
@ -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);
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
|
Loading…
Reference in a new issue