如何从 Android 调用 Java/Kotlin GCP Cloud Functions
How to call Java/Kotlin GCP Cloud Functions from Android
我用kotlin写了一个http函数。从 GCP 测试时它工作正常。相同的几行是:
class WinnerDeclaration : HttpFunction {
override fun service(request: HttpRequest?, response: HttpResponse?) {
//code ...
}
}
参考了https://firebase.google.com/docs/functions/callable#call_the_function,我写了这段代码:
private val cloudFunctions = FirebaseFunctions.getInstance()
val data = hashMapOf("uid" to uid)
cloudFunctions
.getHttpsCallable("WinnerDeclaration")
.call(data)
.addOnSuccessListener {
//code ...
}.addOnFailureListener {
it.printStackTrace()
}
但我总是遇到这个异常: com.google.firebase.functions.FirebaseFunctionsException: NOT_FOUND
callable Cloud Functions 使用的 onCall
协议仅针对 Node.js/Firebase Functions SDK 实现。 Google Cloud Functions 支持的任何其他服务器端库都没有实现。
因此,如果您想使用用 Java/Kotlin 编写的可调用 Cloud Functions,您必须自己实现 onCall
protocol 的服务器端版本。
我用kotlin写了一个http函数。从 GCP 测试时它工作正常。相同的几行是:
class WinnerDeclaration : HttpFunction {
override fun service(request: HttpRequest?, response: HttpResponse?) {
//code ...
}
}
参考了https://firebase.google.com/docs/functions/callable#call_the_function,我写了这段代码:
private val cloudFunctions = FirebaseFunctions.getInstance()
val data = hashMapOf("uid" to uid)
cloudFunctions
.getHttpsCallable("WinnerDeclaration")
.call(data)
.addOnSuccessListener {
//code ...
}.addOnFailureListener {
it.printStackTrace()
}
但我总是遇到这个异常: com.google.firebase.functions.FirebaseFunctionsException: NOT_FOUND
callable Cloud Functions 使用的 onCall
协议仅针对 Node.js/Firebase Functions SDK 实现。 Google Cloud Functions 支持的任何其他服务器端库都没有实现。
因此,如果您想使用用 Java/Kotlin 编写的可调用 Cloud Functions,您必须自己实现 onCall
protocol 的服务器端版本。