简体   繁体   中英

Invoke AWS Lambda function on Android

I'm learning Java and I'm trying to make a simple Android App, the point is to use Amazon Web Services Cognito service to login, and then, run a lambda function as simple as an Hello World.

I followed the AWS but seems to be outdated.

So far I have this:

package com.mtrigen.mtrigenunit;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.mobile.auth.core.IdentityHandler;
import com.amazonaws.mobile.auth.core.IdentityManager;
import com.amazonaws.mobile.auth.ui.SignInUI;
import com.amazonaws.mobile.client.AWSMobileClient;
import com.amazonaws.mobile.client.AWSStartupHandler;
import com.amazonaws.mobile.client.AWSStartupResult;
import com.amazonaws.mobile.config.AWSConfiguration;
import com.amazonaws.mobileconnectors.lambdainvoker.*;
import com.amazonaws.auth.CognitoCachingCredentialsProvider;
import com.amazonaws.regions.Regions;

public class AuthenticatorActivity extends AppCompatActivity {

    private AWSCredentialsProvider credentialsProvider;
    private AWSConfiguration configuration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_authenticator);

        AWSMobileClient.getInstance().initialize(this, new AWSStartupHandler() {
            @Override
            public void onComplete(AWSStartupResult awsStartupResult) {

                credentialsProvider = AWSMobileClient.getInstance().getCredentialsProvider();
                configuration = AWSMobileClient.getInstance().getConfiguration();

                IdentityManager.getDefaultIdentityManager().getUserID(new IdentityHandler() {
                    @Override
                    public void onIdentityId(String identityId) {
                        Log.d("AuthenticatorActivity", "Identity ID is: " + identityId);
                        Log.d("AuthenticatorActivity", "Catched Identity ID: " + IdentityManager.getDefaultIdentityManager().getCachedUserID());
                    }

                    @Override
                    public void handleError(Exception exception) {
                        Log.e("AuthenticatorActivity", "Error in retrieving Identity ID: " + exception.getMessage());

                    }
                });
                SignInUI signin = (SignInUI) AWSMobileClient.getInstance().getClient(AuthenticatorActivity.this, SignInUI.class);
                signin.login(AuthenticatorActivity.this, NextActivity.class).execute();
            }
        }).execute();
        LambdaInvokerFactory.Builder factory = new LambdaInvokerFactory.Builder().context(getApplicationContext()).region(Regions.US_EAST_1).credentialsProvider(credentialsProvider).awsConfiguration(configuration).build();
    }
}

When running, the app show me a simple Login UI, everything on the AWS configuration is good, as I can create users and login, the problem is on the LambdaInvokerFactory, Android Studio told me is deprecated, so I use Builder(), however, I get an error saying that Builder() has protected access on 'com.amazonaws.mobileconnectors.lambdainvoker.LambdaInvokerFactory.Builder()'

Any ideas on how to proceed?, I'm following this documentation: https://docs.aws.amazon.com/aws-mobile/latest/developerguide/how-to-android-lambda.html?shortFooter=true

You can do that by using the following code:

// Create an instance of CognitoCachingCredentialsProvider
CognitoCachingCredentialsProvider cognitoProvider = new CognitoCachingCredentialsProvider(
        this.getApplicationContext(), "identity-pool-id", Regions.US_WEST_2);

// Create LambdaInvokerFactory, to be used to instantiate the Lambda proxy.
LambdaInvokerFactory factory = new LambdaInvokerFactory(this.getApplicationContext(),
        Regions.US_WEST_2, cognitoProvider);

// Create the Lambda proxy object with a default Json data binder.
// You can provide your own data binder by implementing
// LambdaDataBinder.
final MyInterface myInterface = factory.build(MyInterface.class);

RequestClass request = new RequestClass("John", "Doe");
// The Lambda function invocation results in a network call.
// Make sure it is not called from the main thread.
new AsyncTask<RequestClass, Void, ResponseClass>() {
    @Override
    protected ResponseClass doInBackground(RequestClass... params) {
        // invoke "echo" method. In case it fails, it will throw a
        // LambdaFunctionException.
        try {
            return myInterface.AndroidBackendLambdaFunction(params[0]);
        } catch (LambdaFunctionException lfe) {
            Log.e("Tag", "Failed to invoke echo", lfe);
            return null;
        }
    }

    @Override
    protected void onPostExecute(ResponseClass result) {
        if (result == null) {
            return;
        }

        // Do a toast
        Toast.makeText(MainActivity.this, result.getGreetings(), Toast.LENGTH_LONG).show();
    }
}.execute(request);

For detailed instructions and setup, please follow the following link: https://docs.aws.amazon.com/lambda/latest/dg/with-ondemand-android-mobile-create-app.html

Thanks, Rohan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM