【问题标题】:access https cloud function end point via android retrofit通过android改造访问https云功能端点
【发布时间】:2021-07-05 21:01:12
【问题描述】:

我正在尝试使用 Android 中的改造从云功能获得响应。

不知道有没有必要提一下,但是在functions/index.js里面除了下图还有其他的函数。

以下是我如何编写云函数端点:

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

const admin = require('firebase-admin');
admin.initializeApp();

app.post('/shipping', (req, res) => {
  console.log('Inside https request for shipping')
  res.status(200).send('Helo from express post');
})

exports.shipping = functions.https.onRequest(app);

以下是我如何从 Android 调用终点:

FirebaseApi

public interface FirebaseApi {
    @POST("shipping")
    Call<String> getShippingPrice();
}

请求:

private void sendRequestToFirebaseCloudFunctions() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://us-central1-<your-project-id>.cloudfunctions.net/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        FirebaseApi firebaseApi = retrofit.create(FirebaseApi.class);
        Call<String> call = firebaseApi.getShippingPrice();
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Log.i(TAG, "onResponse: isSuccessful "+response.isSuccessful());
                Log.i(TAG, "onResponse: body: "+response.body());
                Log.i(TAG, "onResponse: errorBody: "+response.errorBody());
                try {
                    Log.i(TAG, "onResponse: error: "+response.errorBody().string());
                } catch (IOException e) {
                    Log.i(TAG, "onResponse: catherror: "+e.getMessage());

                }

            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Log.i(TAG, "onFailure: "+t.getMessage());
            }
        });
    }

日志如下所示:

onResponse: isSuccessful false
onResponse: body: null
onResponse: errorBody: okhttp3.ResponseBody$1@3e75b8d
onResponse: error: <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>Error</title>
    </head>
    <body>
    <pre>Cannot POST /</pre>
    </body>
    </html>

我怎样才能让它工作?

我看过一些添加 CORS 的教程。即使我的应用已连接到 firebase 并使用 firestore 和实时数据库,我是否真的需要 CORS?

感谢您的帮助!

【问题讨论】:

    标签: android google-cloud-functions retrofit2


    【解决方案1】:

    使用您当前的代码,正确的 POST 路径是:

    https://us-central1-<your-project-id>.cloudfunctions.net/shipping/shipping
    

    这是因为你将函数导出为shipping,但也只监听相对路径/shipping上的POST请求

    【讨论】:

    • 它在邮递员中有效,但在 Android 中无效。 onFailure: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $
    • @AlitonOliveira 您目前发回纯文本,而不是 JSON。如果要发送 JSON,请使用 res.status(200).json('Helo from express post')
    • 成功了。最后一个问题:我如何创建一个只是/shipping 而不是/shipping/shipping 的路径?
    • 只使用 app.post('/', ...) 或将其托管在 Firebase 托管之后。然后,您可以使用 api.your domain.com 之类的东西来代替云函数 URL。
    猜你喜欢
    • 2018-06-24
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多