When i send a Message to a Amazon Device using AmazonDeviceMessaging api i get an Intent with a Bundle.
for ex.
They are created by
key1 and key2 can be any string here. Even more keys.
How can i get the hole Bundle-Content as a json-String for example?
Solution:
for ex.
adm_message_md5 is fix (came from the api), the others are dynamic.Bundle[{key1=Value1, key2=Value2, adm_message_md5=VZn6hl0RudkG3KZEm+kLCA==}]
They are created by
B4X:
Dim data As Map
data.Initialize
data.Put("key1","Value1")
data.Put("key2","Value2")
jsonmap.Put("data",data)
How can i get the hole Bundle-Content as a json-String for example?
Solution:
B4X:
private String getJson(final Bundle bundle) {
if (bundle == null) return null;
JSONObject jsonObject = new JSONObject();
for (String key : bundle.keySet()) {
Object obj = bundle.get(key);
try {
jsonObject.put(key, wrap(bundle.get(key)));
} catch (JSONException e) {
e.printStackTrace();
}
}
return jsonObject.toString();
}
public static Object wrap(Object o) {
if (o == null) {
return JSONObject.NULL;
}
if (o instanceof JSONArray || o instanceof JSONObject) {
return o;
}
if (o.equals(JSONObject.NULL)) {
return o;
}
try {
if (o instanceof Collection) {
return new JSONArray((Collection) o);
}
if (o instanceof Map) {
return new JSONObject((Map) o);
}
if (o instanceof Boolean ||
o instanceof Byte ||
o instanceof Character ||
o instanceof Double ||
o instanceof Float ||
o instanceof Integer ||
o instanceof Long ||
o instanceof Short ||
o instanceof String) {
return o;
}
if (o.getClass().getPackage().getName().startsWith("java.")) {
return o.toString();
}
} catch (Exception ignored) {
}
return null;
}
Last edited: