5

I'm trying to calling a firebase cloud function that I have written.

I have tested the function using Postman to mimic HTTP requests. Here is the JSON result when I call my function within Postman:

{
 "groups": [
    {
        "isPublic": true,
        "members": [
            true
        ],
        "numberOfMembers": 1,
        "groupId": "-LAOPAzMGzOd9qULPxue"
    },
    {
        "isPublic": true,
        "members": [
            true
        ],
        "numberOfMembers": 1,
        "groupId": "-LAOP7ISDI2JPzAgTYGi"
    }
 ]
}

I am attempting to do the same and retrieve this JSON list within my android app. I am following the example on Firebase's website :https://firebase.google.com/docs/functions/callable

This is Firebase's example on how to retrieve the data:

return mFunctions
        .getHttpsCallable("addMessage")
        .call(data)
        .continueWith(new Continuation<HttpsCallableResult, String>() {
            @Override
            public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
                String result = (String) task.getResult().getData();
                return result;
            }
        });

It is unclear how I can take the result from my cloud function and use it in the rest of my Android app.

Furthermore, this example returns a Task object which according to Firebase's documentation has now deprecated : https://firebase.google.com/docs/reference/admin/java/reference/com/google/firebase/tasks/Task)

Is there a clearer, more simple way to handle the data from a function call?

Calling a function is extremely simple so I feel that there must be a more straight forward method to receiving the response.

3
  • Task is deprecated in the Admin SDK for Java, which is what you're linking to. For the Android SDK, Task is still used for all async work, and that's not changing any time soon. Apr 19, 2018 at 16:00
  • Did you find a solution? It must be perfect if they provide method like getValue or toObject which automatically generate model object from retrieved snapshop in RTDB and Firestore. It seems task.getResult().getData() returns an Object. Does it means it can also be String, Integer, List or something else?
    – wonsuc
    May 9, 2018 at 12:54
  • As an educated guess, if the result string is the string representation of your JSON, you can use Gson to parse the JSON and store the data into a class.
    – KelvZhan
    May 20, 2018 at 23:53

2 Answers 2

7

If you're simply interested in getting JSON from your endpoint, do this from your Activity:

        mFunctions
            .getHttpsCallable("getGroups") //Whatever your endpoint is called
            .call()
            .addOnSuccessListener(this, new OnSuccessListener<HttpsCallableResult>() {
                @Override
                public void onSuccess(HttpsCallableResult httpsCallableResult) {
                    try{
                        Gson g = new Gson();
                        String json = g.toJson(httpsCallableResult.getData());
                        Groups groups = g.fromJson(json,Groups.class);
                    } catch (Exception e){
                        Log.d("Error",e.toString());
                    }
                }
            });
6
  • 1
    Given that most deserialisation frameworks in Android don't like taking Maps as input, it would be nice if Firebase Functions could provide raw json, rather than doing this round-trip to generic objects.
    – joerick
    Jun 4, 2020 at 9:27
  • You can get a raw json as well. You are not forced to deserialize it to a certain data class. @joerick
    – Xenolion
    Aug 18, 2020 at 19:52
  • 1
    @Xenolion how? I couldn't find that in the documentation
    – joerick
    Aug 21, 2020 at 9:33
  • @Xenolion I would also like to know how to get the raw value (as a String)? Sep 14, 2020 at 12:37
  • httpsCallableResult.getData().toString() did not work?? @SvenJacobs
    – Xenolion
    Sep 14, 2020 at 13:27
3

If you're using Kotlinx Serialization, you can use org.json.JsonElement to convert the result data into a format that the serialization framework will accept:

val json = Json(JsonConfiguration.Stable)

fun getPerson(personId: String): Task<Person> {
    val function = functions.getHttpsCallable("getPersonInfo")

    return function.call(mapOf("personId" to personId)).continueWith { task ->
        val result = task.result ?: throw Exception("result is null")

        val jsonString = org.json.JSONObject(result.data as Map<*, *>).toString()
        json.parse(Person.serializer(), jsonString)
    }
}
1
  • The snippet "json.parse(Person.serializer(), jsonString)" didn't work. Replace it with "Json.parseToJsonElement(jsonString)"
    – Roshan
    Feb 2, 2022 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.