简体   繁体   English

在Android上调用AWS Lambda函数

[英]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. 我正在学习Java,并且正在尝试制作一个简单的Android应用程序,重点是使用Amazon Web Services Cognito服务登录,然后运行一个像Hello World一样简单的lambda函数。

I followed the AWS but seems to be outdated. 我遵循了AWS,但似乎已经过时了。

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()' 运行时,该应用程序向我显示了一个简单的Login UI,AWS配置上的所有内容都很好,因为我可以创建用户并登录,问题出在LambdaInvokerFactory上,Android Studio告诉我已弃用,所以我使用Builder(),但是,我收到一条错误消息,说Builder()已保护对'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 有关如何进行的任何想法?,我正在关注此文档: 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 有关详细说明和设置,请访问以下链接: https : //docs.aws.amazon.com/lambda/latest/dg/with-ondemand-android-mobile-create-app.html

Thanks, Rohan 谢谢,罗汉

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JsonReaderException,从Android调用AWS lambda函数时 - JsonReaderException , when invoke AWS lambda function from Android 如何在 ZD52387880E1EA223817A 中的另一个 aws lambda 调用 aws lambda function - How to invoke an aws lambda function from another aws lambda in Java? 如何让 AWS API 网关在 AWS Lambda function 中调用 URL? - How to get AWS API Gateway invoke URL in an AWS Lambda function? 如何从Java代码调用AWS lambda函数/处理程序 - How to invoke the AWS lambda function / handler from Java code 无法在使用 MacOS Catalina 的 AWS Lambda function 中原生调用 Quarkus 函数 - Unable to Invoke Quarkus Functions Natively in an AWS Lambda function with MacOS Catalina AWS-Lambda Scheduler调用多个Lambda实例 - AWS - Lambda Scheduler to invoke multiple lambda instances 如何使用 java 代码调用在 SAM localhost:3001 上运行的 AWS lambda function - How can I invoke a AWS lambda function running on SAM localhost:3001 using java code 无法在本地调用 Java 上的 Lambda AWS - Cannot invoke Lambda AWS on java locally 如何在Java中创建/调用AWS Lambda函数? - How to create/invoke AWS Lambda functions in Java? 无法使用Serverless在本地调用AWS Lambda - Not able to invoke locally AWS Lambda using Serverless
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM