use safeGetString
This commit is contained in:
parent
601dbc1036
commit
fc070c95a7
3 changed files with 15 additions and 4 deletions
|
@ -63,6 +63,7 @@ import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
|
|||
import info.nightscout.androidaps.plugins.Overview.notifications.Notification;
|
||||
import info.nightscout.utils.DateUtil;
|
||||
import info.nightscout.utils.FabricPrivacy;
|
||||
import info.nightscout.utils.JsonHelper;
|
||||
import info.nightscout.utils.SP;
|
||||
import io.socket.client.IO;
|
||||
import io.socket.client.Socket;
|
||||
|
@ -351,7 +352,7 @@ public class NSClientService extends Service {
|
|||
}
|
||||
if (Config.detailedLog)
|
||||
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) {
|
||||
FabricPrivacy.logException(e);
|
||||
}
|
||||
|
@ -577,9 +578,7 @@ public class NSClientService extends Service {
|
|||
// remove from upload queue if Ack is failing
|
||||
UploadQueue.removeID(jsonFood);
|
||||
|
||||
String action = null;
|
||||
if (jsonFood.has("action"))
|
||||
action = jsonFood.getString("action");
|
||||
String action = JsonHelper.safeGetString(jsonFood, "action");
|
||||
|
||||
if (action == null) {
|
||||
addedFoods.put(jsonFood);
|
||||
|
|
|
@ -27,6 +27,16 @@ public class JsonHelper {
|
|||
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 {
|
||||
double result = 0d;
|
||||
|
||||
|
|
|
@ -19,6 +19,8 @@ public class JsonHelperTest {
|
|||
JSONObject object = new JSONObject(jsonString);
|
||||
assertEquals(null, JsonHelper.safeGetString(object, "notexisting"));
|
||||
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(3.0d, JsonHelper.safeGetDouble(object, "d"), 0.000001d);
|
||||
|
|
Loading…
Reference in a new issue