繁体   English   中英

在Android上调用AWS Lambda函数

[英]Invoke AWS Lambda function on Android

我正在学习Java,并且正在尝试制作一个简单的Android应用程序,重点是使用Amazon Web Services Cognito服务登录,然后运行一个像Hello World一样简单的lambda函数。

我遵循了AWS,但似乎已经过时了。

到目前为止,我有这个:

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();
    }
}

运行时,该应用程序向我显示了一个简单的Login UI,AWS配置上的所有内容都很好,因为我可以创建用户并登录,问题出在LambdaInvokerFactory上,Android Studio告诉我已弃用,所以我使用Builder(),但是,我收到一条错误消息,说Builder()已保护对'com.amazonaws.mobileconnectors.lambdainvoker.LambdaInvokerFactory.Builder()'的访问

有关如何进行的任何想法?,我正在关注此文档: https : //docs.aws.amazon.com/aws-mobile/latest/developerguide/how-to-android-lambda.html?shortFooter=true

您可以使用以下代码来做到这一点:

// 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);

有关详细说明和设置,请访问以下链接: https : //docs.aws.amazon.com/lambda/latest/dg/with-ondemand-android-mobile-create-app.html

谢谢,罗汉

暂无
暂无

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

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