簡體   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