4

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?

3
  • Those objects are ready for your use there, passed as parameters. I couldn't understand what you mean by saying how do I use those objects. Commented Aug 14, 2018 at 13:17
  • I need a parcelable or something right so that the java understands it Commented Aug 14, 2018 at 13:21
  • @Julia check my answer.. It's exactly what you need. Commented Aug 14, 2018 at 13:32

3 Answers 3

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...
Sign up to request clarification or add additional context in comments.

Comments

1

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();

3 Comments

Why cast to HashMap? This is what the ReadableMap interface is for...
@HedeH ReadableMap is implemented for React Native, it didn't exist in Java before. For example, if you are going to pass this object to another page, you will need to have another import from react bridge package to use it. I conventionally cast it to HashMap for using standard util objects of Java in every page. As I said, it's just coding convention, it's up to you.
Fair enough, But the question is about React Native.. And React Native has it's conventions.. Would you use Log.d() when developing for Android or System.out.println()? of course Log.d()....
0

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.