'React native communication between React native and android with custom object
I have a react native component that communicates with Android like this -
My React Native code-
const myModule = NativeModules.MyProcessor;
myModule.processData(obj1, obj2);
My Android code in MyProcessor.java
is -
@ReactMethod
public void processData(final Object1 obj1, final Objet2 obj2) {
//Do something here
}
My object data looks like -
"data1": {
"id": "1",
"name": "john",
"key": "xxxxxxxx"
},
"details": {
"detailId": "2",
"detailName": "peter",
"counter": 1
}
I get the 2 objects data1 and details out of which I need both and also detailId.
My question is how do I use these 2 objects in my java?
Solution 1:[1]
Question:
How do I use these 2 objects Object1 and Object2 in my java?
Answer: Quoted from here:
"The following argument types are supported for methods annotated with @ReactMethod and they directly map to their JavaScript equivalents"
Boolean -> Bool
Integer -> Number
Double -> Number
Float -> Number
String -> String
Callback -> function
ReadableMap -> Object
ReadableArray -> Array
React Native is converting the javascript object into a ReadableMap
.
So instead of expecting an Object
in the native module, You should implement the method like this:
@ReactMethod
public void processData(final ReadableMap obj1, final ReadableMap obj2) {
// Parse the ReadableMap using the available interface methods
}
Those are the available methods in the ReadableMap
interface: Source
public interface ReadableMap {
boolean hasKey(String name);
boolean isNull(String name);
boolean getBoolean(String name);
double getDouble(String name);
int getInt(String name);
String getString(String name);
ReadableArray getArray(String name);
ReadableMap getMap(String name);
Dynamic getDynamic(String name);
ReadableType getType(String name);
ReadableMapKeySetIterator keySetIterator();
HashMap<String, Object> toHashMap();
}
UPDATE
Following the example object you posted, you can parse your data like this:
ReadableMap data1 = obj1.getMap("data1");
String id = data1.getString("id");
String name = data1.getString("name");
// And so on...
ReadableMap details = obj1.getMap("details");
String detailId = details.getString("detailId");
int counter = details.getInt("counter");
// And so on...
Solution 2:[2]
If you want to pass complex objects from React-Native to Android, you should use ReadableMap
, which is used in JavaScript bridge and equivalent to a HashMap
.
ReadableMap obj1
Then, you can cast this to a HashMap and easily get the values
HashMap map = (HashMap) obj1;
String field = map.get("key").toString();
For the specific values you provided:
Get data1 as another HashMap from obj1:
HashMap details = (HashMap) map.get("details");
Read strings from details map:
String detailId = details.get("detailId").toString();
Solution 3:[3]
Send Map or object from react native to android native
const yourMap = { "param1": "value1",
"param2": "value2",
};
const strValue = "hello";
MainModule.methodName(strValue, yourMap)
Receive in android side
@ReactMethod
fun methodName(
strValue: String,
info: ReadableMap,
promise: Promise
) {
mPickerPromise = promise
var hashmap = Utils.toHashMap(info)
// do your stuff here
}
Where Utils.toHashMap()
fun toHashMap(map: ReadableMap?): HashMap<String, Any?> {
val hashMap: HashMap<String, Any?> = HashMap()
val iterator = map!!.keySetIterator()
while (iterator.hasNextKey()) {
val key = iterator.nextKey()
when (map.getType(key)) {
ReadableType.Null -> hashMap[key] = null
ReadableType.Boolean -> hashMap[key] = map.getBoolean(key)
ReadableType.Number -> hashMap[key] = map.getDouble(key)
ReadableType.String -> hashMap[key] = map.getString(key)
ReadableType.Map -> hashMap[key] = toHashMap(map.getMap(key))
else -> throw IllegalArgumentException("Could not convert object with key: $key.")
}
}
return hashMap
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | Quick learner |