简体   繁体   English

Google Cloud Functions + Android应用

[英]Google Cloud Functions + Android app

I followed this Google Cloud Function tutorial - https://firebase.google.com/docs/functions/get-started 我遵循了此Google Cloud Function教程-https: //firebase.google.com/docs/functions/get-started

And I was able to deploy the function through node.js on my Mac running Sierra. 而且我能够在运行Sierra的Mac上通过node.js部署该功能。 I am now successfully able to call my addMessage Http Endpoint from a browser, and it successfully adds the message onto my real-time database in the Firebase console. 现在,我可以从浏览器成功调用addMessage Http端点,它可以将消息成功添加到Firebase控制台中的实时数据库中。

I have also implemented Google Sign In in my app as per this tutorial: https://developers.google.com/identity/sign-in/android/start-integrating 我还按照以下教程在我的应用中实现了Google登录: https//developers.google.com/identity/sign-in/android/start-integrating

However, I am unable to call the addMessage() function from my android app through a standard Http Request. 但是,我无法通过标准的Http请求从我的android应用中调用addMessage()函数。 This is my android code: 这是我的android代码:

private class HttpSenderTask extends AsyncTask { 私有类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);
    }
}

What am I doing wrong? 我究竟做错了什么?

Also, is there some documentation that can teach us how to write android code for utilizing Cloud Functions? 另外,是否有一些文档可以教我们如何编写使用Cloud Functions的android代码? Any help in this matter would be greatly appreciated. 在这方面的任何帮助将不胜感激。

Thank you all 谢谢你们

This line is probably where you're going wrong: 这行可能是您出问题的地方:

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

You need to build a URL to the full URL to the HTTPS endpoint exposed by Cloud Functions. 您需要构建一个URL,以指向Cloud Functions公开的HTTPS端点的完整URL You can get this from the CLI after it deploys your function. 部署功能后,您可以从CLI中获得此信息。 It will involve your Firebase project name as part of the host name in the URL. 它会将您的Firebase项目名称作为URL中主机名的一部分。

Also note that any query string parameters should be escaped, unless you're sure they're safe for HTTP parameters. 还要注意,除非您确定它们对于HTTP参数是安全的,否则任何查询字符串参数都应转义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM