正如弗兰克所说,最好在 StackOveflow 上提问时包含您已经编写的所有代码。
但是,根据您的评论,我了解到您指的是 documentation 中的代码 sn-p(复制/粘贴在下面),并且您对 data.put 有疑问。
private Task<String> addMessage(String text) {
// Create the arguments to the callable function.
Map<String, Object> data = new HashMap<>();
data.put("text", text);
data.put("push", true);
return mFunctions
.getHttpsCallable("addMessage")
.call(data)
.continueWith(new Continuation<HttpsCallableResult, String>() {
@Override
public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
// This continuation runs on either success or failure, but if the task
// has failed then getResult() will throw an Exception which will be
// propagated down.
String result = (String) task.getResult().getData();
return result;
}
});
}
此 Java 代码 sn-p 显示传递(发送)到 Callable Cloud Function 的数据包含在名为 data 的 HashMap 中。
你会在网上找到很多关于如何使用 HashMap 的教程,但简而言之:
“Java HashMap 是 Java 的 Map 接口的基于哈希表的实现。您可能知道,Map 是键值对的集合。它将键映射到值。”来源:https://www.callicoder.com/java-hashmap/
向 HashMap 添加新的键值对的一种方法是使用 put() 方法。所以sn-p中的这部分代码是关于将数据添加到HashMap中,然后发送到CloudFunction。
在 Cloud Function 中,您将获得如下数据(如文档中所述):
exports.addMessage = functions.https.onCall((data, context) => {
// ...
const text = data.text;
const push = data.push;
// ...
});