简体   繁体   English

Firebase功能导致Android应用程序

[英]Firebase functions result in android app

I am trying to show the result of firebase function in the android application . 我试图在android应用程序中显示firebase函数的结果。 It is my first function to practice .I deployed my function correctly and works fine in console. 这是我练习的第一个功能。我正确部署了我的功能并在控制台中正常工作。 then the next step I want is: how to show the result of my function in an empty activity of Android ?? 然后我想要的下一步是:如何在Android的空活动中显示我的函数的结果? or what are steps to connect between functions and android app this is my simple function: 或者什么是连接功能和Android应用程序的步骤这是我的简单功能:

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

// Create and Deploy Your First Cloud Functions

// https://firebase.google.com/docs/functions/write-firebase-functions

exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
 });

To communicate with firebase http cloud function from android app, you need to use any http client library like OkHttp. 要从Android应用程序与firebase http云功能进行通信,您需要使用任何http客户端库,如OkHttp。

You can get the URL of your cloud function from firebase console. 您可以从firebase控制台获取云功能的URL。

OkHttpClient httpClient = new OkHttpClient();
            HttpUrl.Builder httpBuider =
                    HttpUrl.parse(FIREBASE_CLOUD_FUNCTION_URL).newBuilder();

            Request request = new Request.Builder().
                    url(httpBuider.build()).build();

            httpClient.newCall(request).enqueue(new Callback() {
                @Override public void onFailure(Call call, IOException e) {
                    Log.e(TAG, "error in getting response from firebase cloud function");
                }
                @Override public void onResponse(Call call, Response response){
                    ResponseBody responseBody = response.body();
                    String resp = "";
                    if (!response.isSuccessful()) {
                        Log.e(TAG, "fail response from firebase cloud function");
                    }else {
            Log.e(TAG, "respone "+resp);
                    }

                }
            });
    }

For complete example, see firebase cloud functions example 有关完整示例,请参阅firebase云功能示例

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

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