我尝试调用云函数并传递一些数据。我的数据类型为 Map<String, Object>
里面又嵌套了一些 Map<String, Object>
.当试图用这个调用我的函数时:
getHttpsCallable(function).call(data)...
我收到这个错误:
Cloud Function Exception:
java.lang.IllegalArgumentException: Object cannot be encoded in JSON: [Ljava.util.Map;@42896770
at com.google.firebase.functions.Serializer.encode(Serializer.java:136)
at com.google.firebase.functions.Serializer.encode(Serializer.java:77)
at com.google.firebase.functions.FirebaseFunctions.call(FirebaseFunctions.java:255)
at com.google.firebase.functions.FirebaseFunctions.access$100(FirebaseFunctions.java:34)
at com.google.firebase.functions.FirebaseFunctions$2.then(FirebaseFunctions.java:233)
at com.google.firebase.functions.FirebaseFunctions$2.then(FirebaseFunctions.java:225)
at com.google.android.gms.tasks.zzd.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
当打印我的数据对象时,它看起来像这样:
{items=[Ljava.util.Map;@42896770, element1=OzNGHzHeq6av03hRmMJ6, time=Sun May 05 04:40:00 MEZ 3918}
我尝试了以下方法: 我尝试在我的数据对象中打包一个数组而不是一个 Map。 => 没有用。
我试过自己将数据转成json发送,还是不行。
我做了以下工作:
Gson gson = new Gson();
String dataJSON = gson.toJson(data);
结果是一个格式良好的 JSON:
{"items":[{"count":1,"itemId":"94To3bKnxoWzayLdy55I"},{"count":3,"itemId":"fkNtIUxbfg5GhLJy2Dma"},{"count":1,"itemId":"sDYTp7cpQTRfWuI6CSaK"},{"count":1,"itemId":"qGGl5U0qihHnMzhBMjyb","options":["2.0","1.0","1.1","0.0"]},{"count":1,"itemId":"qGGl5U0qihHnMzhBMjyb","options":["2.0","1.0","1.1","0.1"]},{"count":1,"itemId":"qGGl5U0qihHnMzhBMjyb","options":["2.0","1.0","1.1","0.2"]},{"count":1,"itemId":"qGGl5U0qihHnMzhBMjyb","options":["2.3","1.0","1.1","0.1"]}],"storeId":"OzNGHzHeq6av03hRmMJ6","pickupTime":"May 5, 3918 4:40:00 AM"}
但我也尝试使用硬编码的 JSON 调用我的函数,但这也不起作用。
我不知道我做错了什么。有人知道吗?
最佳答案
问题是你实际上传递了 array
的 Map
对象。
由于 Frederik Byl 展示了 Functions SDK for Android 的源代码,它不接受数组类型。它接受 Map
或 List
Java 集合对象。
所以你必须改变你的[Ljava.util.Map;
至 List<Map>
类型。
此外,Firestore 实际上使用 Protobuf对于核心数据集。您可以在 here 找到更多信息。他们如何使用 Protobuf 数据编码或解码 json。
关于android - Firebase:将数据传递给 cloudfunction 导致:对象无法在 JSON 中编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50138130/