I have an server that runs mongoDB as my backend database. In one of the rest APIs, I'm returning the ObjectId to my android app.
In javascript, it looks like this: "_id" : ObjectId("570957abc002e4a2bd56c57f")
.
In JSON, it looks like this:
"my_object_id": {
"timestamp": 1459713776,
"machineIdentifier": 12583652,
"processIdentifier": 28379,
"counter": 9579241,
"date": 1459713776000,
"timeSecond": 1459713776,
"time": 1459713776000
},
I don't know how to convert this back to ObjectId in android.
Of course, android does not contain this object natively so the only way you could get it, I guess, is to import the mongo-java-driver
since there is no mongo-android-driver
available.
I also tried to import the class directly into android by copying the class from the java code into my android app but it doesn't seem to work as it is missing java/lang/management/ManagementFactory
as discussed in this thread as well: mongodb 3.x driver Android compatibility
The objectId class that makes direct reference to the java/lang/management/ManagementFactory
class : https://github.com/mongodb/mongo-java-driver/blob/8810c0a2e379fea40b6388c8a5aad31aa32aed82/bson/src/main/org/bson/types/ObjectId.java
I'm a bit apprehensive about importing an entire mongo-java-driver package into my code as I just need the ObjectId representation and not any of the other methods - is there a way to convert the JSON ObjectId
to an mongoDB ObjectId
object so I can work on it in my android app?