Google Cloud Functions + Android应用

问题描述 投票:0回答:1

我按照谷歌云功能教程 - https://firebase.google.com/docs/functions/get-started

我能够通过运行Sierra的Mac上的node.js部署该功能。我现在能够成功地从浏览器调用我的addMessage Http端点,并成功将消息添加到Firebase控制台中的实时数据库中。

我已根据本教程在我的应用中实施了Google登录:https://developers.google.com/identity/sign-in/android/start-integrating

但是,我无法通过标准的Http请求从我的Android应用程序调用addMessage()函数。这是我的android代码:

私有类HttpSenderTask扩展AsyncTask {

    String testString;
    String content;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        testString = text.getText().toString();
    }

    @Override
    protected String doInBackground(String... strings) {

        try{
            URL url = new URL("**myCloudFunctionlinkFromFirebaseConsole**/addMessage?text=" + testString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            connection.connect();

            content = "Done";
        }
        catch (Exception ex){
            Log.d(TAG, ex.toString());
        }
        return  content;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
    }
}

我究竟做错了什么?

还有,是否有一些文档可以教我们如何编写Android代码以利用云功能?任何有关此事的帮助将不胜感激。

谢谢你们

android firebase google-cloud-functions
1个回答
0
投票

这条线可能是你出错的地方:

URL url = new URL("myCloudFunctionlink/addMessage?text=" + testString);

您需要构建一个URL,指向Cloud Functions公开的HTTPS端点的完整URL。您可以在部署函数后从CLI获取此信息。它会将您的Firebase项目名称作为URL中主机名的一部分。

另请注意,除非您确定它们对HTTP参数是安全的,否则应对任何查询字符串参数进行转义。

© www.soinside.com 2019 - 2024. All rights reserved.