Search code examples
androidamazon-web-servicesaws-lambdaamazon-cognitoaws-amplify

Connection between Android and AWS SageMaker with AWS Lambda


I set up a connection between Android and AWS Lambda which has the endpoint set to SageMaker. I am using the REST API during the connection, the AWS Cognito plug is set to be accessed without authorization.

I make a connection as described here: https://aws.amazon.com/blogs/machine-learning/call-an-amazon-sagemaker-model-endpoint-using-amazon-api-gateway-and-aws-lambda/

My question is how to send this data:

{"data":"13.49,22.3,86.91,561.0,0.08752,0.07697999999999999,0.047510000000000004,0.033839999999999995,0.1809,0.057179999999999995,0.2338,1.3530000000000002,1.735,20.2,0.004455,0.013819999999999999,0.02095,0.01184,0.01641,0.001956,15.15,31.82,99.0,698.8,0.1162,0.1711,0.2282,0.1282,0.2871,0.06917000000000001"}

And how to view the received response later. Anyone know how to do it or where I can find tips on how to do it?


Solution

  • If I understand correctly, this is your system flow:

    1. POST some data from your Android device
    2. It gets received by API Gateway
    3. And continues through to AWS Lambda
    4. In AWS Lambda the data is extracted, and passed to Sage Maker

    Creating a POST using AWS Amplify

    To POST data form the Android device, follow the Amplify API (REST) category documentation.

    Specifically, you can do something like:

    val options = RestOptions.builder()
        .addPath("/prod/predictbreastcancer")
        .addBody("{\"data\":\"13.49,22.3,86.91,561.0,0.08752,0.07697999999999999,0.047510000000000004,0.033839999999999995,0.1809,0.057179999999999995,0.2338,1.3530000000000002,1.735,20.2,0.004455,0.013819999999999999,0.02095,0.01184,0.01641,0.001956,15.15,31.82,99.0,698.8,0.1162,0.1711,0.2282,0.1282,0.2871,0.06917000000000001\"}".toByteArray())
        .build()
    
    Amplify.API.post(options,
        { Log.i("Demo", "POST response = $it") },
        { Log.e("Demo", "POST failed", it) }
    )
    

    Creating POST body from EditText content

    You mentioned you're using an EditText widget to gather the input data. I assume a user can enter a comma-separated list of values like 0.44, 6.11, etc.

    To extract it's content and build the POST body, you can do:

    val input = findViewById(R.id.input) as EditText
    val body = JSONObject()
        .put("data", input.text)
        .toString()
        .replaceAll("\"", "\\\"")
    

    Displaying response in another Activity

    Skimming the blog you referenced, I can't see an example of the response body content. But, here's how you can retrieve response JSON and pass it to a new activity.

    Amplify.API.post(options,
        { response ->
            val intent = Intent(this, YourOtherActivity::class.java)
            intent.putExtra("json", response.data.asString())
            runOnUiThread { startActivity(intent) }
        },
        { /* handle error ... */ }
    )
    

    In YourOtherActivity, access the extra data in onCreate() like so:

    val json = intent.getStringExtra("json")