YoanGJ
YoanGJ

Reputation: 509

AWS Lambda always returns null on Android

I'm building an app on iOS and Android using AWS Amplify. I have a lambda expression that takes a longitude, a latitude, a username, an object id and returns me a boolean that tells me if yes or no the user is physically close to this object.

It works perfectly on my iOS app, I get true or false depending on the user location but on Android I always get null as a response.

Here's my function that triggers my lambda:

fun locationLambda() {
    if ( ContextCompat.checkSelfPermission( this, Manifest.permission.ACCESS_FINE_LOCATION ) == PackageManager.PERMISSION_GRANTED ) {
        fusedLocationClient.lastLocation.addOnSuccessListener { location : Location? ->
            if (location != null) {

                val latitude = location.latitude.toString()
                val longitude = location.longitude.toString()
                val username = AWSMobileClient.getInstance().username
                val objectId = (intent.extras.getSerializable("object") as Object).getId()

                Log.i("ObjectId", objectId)
                Log.i("Longitude", longitude)
                Log.i("Latitude", latitude)
                Log.i("Username", username)

                mAWSAppSyncClient?.query(IsObjectFoundQuery.builder()
                        .latitude(latitude)
                        .longitude(longitude)
                        .username(username)
                        .objectID(objectId)
                        .build())
                        ?.responseFetcher(AppSyncResponseFetchers.NETWORK_ONLY)
                        ?.enqueue(locationCallback)
            }
        }
    }
}

When I print in logs the parameters sent to the lambda everything is normal, the right values are sent.

And here's my callback function:

private val locationCallback = object : GraphQLCall.Callback<IsObjectFoundQuery.Data>() {
    override fun onResponse( response: com.apollographql.apollo.api.Response<IsObjectFoundQuery.Data>) {
        Log.i("Results", response.data()?.isObjectFound.toString())
    }

    override fun onFailure(e: ApolloException) {
        Log.e("ERROR", e.toString())
    }
}

Here I successfully get a response but response.data()?.isObjectFound is null

Am I doing something wrong here or do I have to look somewhere else?

Upvotes: 1

Views: 439

Answers (1)

Taterhead
Taterhead

Reputation: 5951

Troubleshooting your lambda function without logging is a bit like finding something in the dark: Its very difficult. You are going to need logging for the client side and the server side (aka backend) to be able to troubleshoot this.

For the backend logging, AWS provides Cloudwatch logs for the Lambda and API Gateway.

So to check your Cloudwatch logs for your lambda, go into your AWS console for the Region where your Lambda is installed and go into the Cloudwatch service. Then select Logs on the side. You should see a log group named for your Lambda function. Clicking on this will show you the diagnostic information collected for your lambda when it is called (see image below for an example). Expand and collapse the log line entries. enter image description here

Once you confirm you have cloudwatch logs turned on, follow this strategy:

  1. Run a test call from the IOS client and confirm the output is successful on the client. Then check the cloudwatch logs (for both the API gateway and the Lambda function) of your IOS test and note what a success looks like.

  2. Next run a second test from Android using your code above. Look in cloudwatch for the differences between the two and this should reveal details to help you troubleshoot what went wrong in your Android call.

If Amplify did not create Cloudwatch logs for your API gateway or Lambda function, follow the steps here in setting them up. Hope this helps you find the solution.

Upvotes: 1

Related Questions