【问题标题】:Cloud Functions - Android application crashe when getHttpsCallable() callCloud Functions - 调用 getHttpsCallable() 时 Android 应用程序崩溃
【发布时间】:2020-11-09 04:49:59
【问题描述】:

我尝试从我的 android 项目调用 firebase 模拟器 中的函数。

我关注https://firebase.google.com/docs/functions/callable#java_2,但是当我调用 getHttpsCallable 时,我的 AVD 停止并且我在 logcat 或事件日志中看不到任何错误。

这里有一个比课程更短的代码,但也不起作用。

在 android Studio 中:(java)

public class MainActivity extends AppCompatActivity {
    private final static String TAG = "DebugTest";

    private DatabaseReference mRef ;
    private FirebaseFunctions mFunctions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button clickBtn = findViewById(R.id.clickBtn);

        // WARNING: firebase emulators
        mRef = FirebaseDatabase
                .getInstance("http://10.0.2.2:9000?ns=nameOfMyApplication-xxxxx")
                .getReference();



        //crashe when I click on the button
        clickBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mFunctions.getHttpsCallable("hello").call();
                        
                    }
        });
    }
}

在 index.js 中

const functions = require('firebase-functions');

exports.hello = functions.https.onCall((data, context) => {
    return `Hello world!`
})

该函数未在 firebase 模拟器套件的日志中调用。

【问题讨论】:

    标签: android firebase-realtime-database google-cloud-functions avd


    【解决方案1】:

    您忘记初始化 mFunctions 变量,您刚刚声明了它。尝试在您已经在进行的 firestore 初始化旁边添加以下代码,它应该可以工作:

    mFunctions = FirebaseFunctions.getInstance();
    

    编辑:

    要记录函数的返回,您必须将 onClick 函数更改为以下内容:

    public void onClick(View v) {
        System.out.println(mFunctions.getHttpsCallable("hello").call().getValue());                      
    }
    

    如果这能解决问题,请告诉我。

    【讨论】:

    • 非常感谢!我的应用程序不会崩溃。但是我在我的模拟器中看不到任何日志。这正常吗??
    • 好吧,鉴于您拥有的代码,是的,因为您没有记录您的云函数的返回,您只是调用该函数并且没有对它在您的 android 代码中的返回做任何事情。我将编辑答案以添加此问题,如果解决了问题,请记住接受并投票:)
    • 函数的输出是一个Task。我有代码错误内部
    • 你是对的,我刚刚修复了代码,现在应该可以工作了。
    • 我无法使用 getValue。此外,当我在 index.js 中使用 console.log 时,日志不可见,所以我认为这是另一个错误。我将创建另一个帖子,因为原始实例问题已解决。
    猜你喜欢
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多