【问题标题】:Firebase functions result in android appFirebase 函数导致 android 应用程序
【发布时间】:2018-03-31 12:07:53
【问题描述】:

我试图在 android 应用程序中显示 firebase 函数 的结果。 这是我练习的第一个功能。我正确部署了我的功能,并且在控制台中运行良好。那么我想要的下一步是:如何在 Android 的空活动中显示我的函数的结果?或者函数和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!");
 });

【问题讨论】:

标签: android firebase google-cloud-functions


【解决方案1】:

要从 Android 应用程序与 firebase http 云功能进行通信,您需要使用任何 http 客户端库,例如 OkHttp。

您可以从 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);
                    }

                }
            });
    }

完整示例见firebase cloud functions example

【讨论】:

    猜你喜欢
    • 2018-09-23
    • 2018-02-11
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    • 2014-05-08
    • 2013-12-02
    • 2014-09-09
    • 1970-01-01
    相关资源
    最近更新 更多