0

所以我有這個lambda函數,它只需要輸入一個用戶名,然後轉到dynamoDB,並抓住該表中的相應用戶及其所有屬性並將其記錄下來。它工作得很好,當我在lambda測試。但是,當我通過SDK調用GET方法不起作用,我不明白爲什麼?如何使用aws api網關在android中調用GET方法?

這裏是lambda中的函數。

AWS.config.update(dynamoDBConfiguration); 

var dynamodb = new AWS.DynamoDB.DocumentClient(); 
exports.handler = function(event, context, callback) 
{ 
    console.log('entered exports handler'); 
    console.log(JSON.stringify(event, null, ' ')); 

    var userTableName = "usersTable"; 

    var params = { 
     TableName : userTableName, 
     Key: { 
      "username": event.username 
     } 
     // AttributesToGet: [ 
     // 'STRING_VALUE', 
     // /* more items */ 
     // ], 
    }; 
    dynamodb.get(params, function(err, data) 
     { if (err) { 
       console.log(err); 
       callback(err); 
      } else { 
       console.log('great success: %j',data); 
       callback(null, data); 
      } 
     }); 
}; 

現在我試着來測試API getway這個功能,所以我可以在以後部署SDK,但有巨大的麻煩搞清楚了這一點IM和希望你們能幫助我了我在想念着什麼!

這裏是我的API enter image description here

接下來,我的方法要求我的GET方法。 enter image description here

我的合併請求 enter image description here

我的方法響應

enter image description here

其次是我的輸出模式

{ 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "type": "object", 
    "properties": { 
     "username": {"type":"string"} 
    }, 
    "title": "Output" 
} 

我的合併響應 enter image description here

這裏是我得到的輸出,當我在Lambda測試(所以工作正常)。

{ 
    "Item": { 
    "experience": "0", 
    "displayName": "Archer", 
    "profileImageRef": "default", 
    "folders": { 
     "My Cards": { 
     "cards": {}, 
     "name": "My Cards" 
     } 
    }, 
    "birthdate": "1/1/1990", 
    "lastName": "Farooqui", 
    "username": "Archer", 
    "email": "[email protected]", 
    "firstName": "Qamar" 
    } 
} 

在android中,這裏是我生成的sdk放入我的android studio。

MyUserClient SDK

@com.amazonaws.mobileconnectors.apigateway.annotation.Service(endpoint = "https://ow2zhiry2b.execute-api.us-west-2.amazonaws.com/awsTest") 

    public interface MyUserClient { 


     /** 
     * A generic invoker to invoke any API Gateway endpoint. 
     * @param request 
     * @return ApiResponse 
     */ 
     com.amazonaws.mobileconnectors.apigateway.ApiResponse execute(com.amazonaws.mobileconnectors.apigateway.ApiRequest request); 

     /** 
     * 
     * 
     * @param username 
     * @return Output 
     */ 
     @com.amazonaws.mobileconnectors.apigateway.annotation.Operation(path = "/", method = "GET") 
     Output rootGet(
       @com.amazonaws.mobileconnectors.apigateway.annotation.Parameter(name = "username", location = "query") 
       String username); 
    } 

下面是從SDK

public class Output { 
    @com.google.gson.annotations.SerializedName("username") 
    private String username = null; 

    /** 
    * Gets username 
    * 
    * @return username 
    **/ 
    public String getUsername() { 
     return username; 
    } 

    /** 
    * Sets the value of username. 
    * 
    * @param username the new value 
    */ 
    public void setUsername(String username) { 
     this.username = username; 
    } 

} 

最後,我嘗試實現異步任務GET方法生成的,以及我OUT輸出類,但它會在日誌語句中崩潰。

class gateWayAsyncTask extends AsyncTask<Void, Void, Void> 
    { 
     private String userName; 
     public gateWayAsyncTask(String userName) 
     { 
      this.userName = userName; 
     } 
     @Override 
     protected Void doInBackground(Void... params) 
     { 
      creatingUser = clientFactory.build(com.awsTest.clientsdk.MyUserClient.class); 
      userName = creatingUser.rootGet("Archer").getUsername(); 
      return null; 
      //clientFactory, and creatingUser variables are assigned in the parent class of this Async task don't worry. 
     } 
     @Override 
     public void onPostExecute(Void var) 
     { 
      Log.d("gateway","gateway succeded!"); 
      Log.d("gateway",userName); 
      Toast.makeText(getBaseContext(),userName,Toast.LENGTH_LONG).show(); 
     } 
    } 

這裏是我的堆棧跟蹤

12-27 12:42:48.929 29019-29019/com.daprlabs.aaron.swipedeck2 E/UncaughtException: java.lang.NullPointerException: println needs a message 
                         at android.util.Log.println_native(Native Method) 
                         at android.util.Log.d(Log.java:139) 
                         at com.daprlabs.aaron.zivitApp.Cognito.LogInZivit$gateWayAsyncTask.onPostExecute(LogInZivit.java:313) 
                         at com.daprlabs.aaron.zivitApp.Cognito.LogInZivit$gateWayAsyncTask.onPostExecute(LogInZivit.java:281) 
                         at android.os.AsyncTask.finish(AsyncTask.java:651) 
                         at android.os.AsyncTask.access$500(AsyncTask.java:180) 
                         at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668) 
                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                         at android.os.Looper.loop(Looper.java:148) 
                         at android.app.ActivityThread.main(ActivityThread.java:5525) 
                         at java.lang.reflect.Method.invoke(Native Method) 
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
12-27 12:42:49.169 29019-29019/com.daprlabs.aaron.swipedeck2 E/AndroidRuntime: FATAL EXCEPTION: main 
                       Process: com.daprlabs.aaron.swipedeck2, PID: 29019 
                       java.lang.NullPointerException: println needs a message 
                        at android.util.Log.println_native(Native Method) 
                        at android.util.Log.d(Log.java:139) 
                        at com.daprlabs.aaron.zivitApp.Cognito.LogInZivit$gateWayAsyncTask.onPostExecute(LogInZivit.java:313) 
                        at com.daprlabs.aaron.zivitApp.Cognito.LogInZivit$gateWayAsyncTask.onPostExecute(LogInZivit.java:281) 
                        at android.os.AsyncTask.finish(AsyncTask.java:651) 
                        at android.os.AsyncTask.access$500(AsyncTask.java:180) 
                        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668) 
                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                        at android.os.Looper.loop(Looper.java:148) 
                        at android.app.ActivityThread.main(ActivityThread.java:5525) 
                        at java.lang.reflect.Method.invoke(Native Method) 
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
12-27 12:43:14.387 29295-29295/com.daprlabs.aaron.swipedeck2 D/gateway: gateway succeded! 
12-27 12:43:14.388 29295-29295/com.daprlabs.aaron.swipedeck2 E/UncaughtException: java.lang.NullPointerException: println needs a message 
                         at android.util.Log.println_native(Native Method) 
                         at android.util.Log.d(Log.java:139) 
                         at com.daprlabs.aaron.zivitApp.Cognito.LogInZivit$gateWayAsyncTask.onPostExecute(LogInZivit.java:313) 
                         at com.daprlabs.aaron.zivitApp.Cognito.LogInZivit$gateWayAsyncTask.onPostExecute(LogInZivit.java:281) 
                         at android.os.AsyncTask.finish(AsyncTask.java:651) 
                         at android.os.AsyncTask.access$500(AsyncTask.java:180) 
                         at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668) 
                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                         at android.os.Looper.loop(Looper.java:148) 
                         at android.app.ActivityThread.main(ActivityThread.java:5525) 
                         at java.lang.reflect.Method.invoke(Native Method) 
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
12-27 12:43:14.604 29295-29295/com.daprlabs.aaron.swipedeck2 E/AndroidRuntime: FATAL EXCEPTION: main 
                       Process: com.daprlabs.aaron.swipedeck2, PID: 29295 
                       java.lang.NullPointerException: println needs a message 
                        at android.util.Log.println_native(Native Method) 
                        at android.util.Log.d(Log.java:139) 
                        at com.daprlabs.aaron.zivitApp.Cognito.LogInZivit$gateWayAsyncTask.onPostExecute(LogInZivit.java:313) 
                        at com.daprlabs.aaron.zivitApp.Cognito.LogInZivit$gateWayAsyncTask.onPostExecute(LogInZivit.java:281) 
                        at android.os.AsyncTask.finish(AsyncTask.java:651) 
                        at android.os.AsyncTask.access$500(AsyncTask.java:180) 
                        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668) 
                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                        at android.os.Looper.loop(Looper.java:148) 
                        at android.app.ActivityThread.main(ActivityThread.java:5525) 
                        at java.lang.reflect.Method.invoke(Native Method) 
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 

UPDATE

所以,當我看着雲手表。它確實表明我的lambda函數被調用並執行。但是,我沒有得到回報價值。所以我的方法響應和/或集成響應有些問題,但我不確定是什麼。

回答

1

看起來您的集成響應中的模板爲空,並且Lambda輸出與您的輸出模型不匹配。在集成響應頁面的Body Mapping Templates下,選擇左側的application/json。在右手邊,輸入模板的身體是這樣的:

 

    { 
     "username": $input.json('$.username') 
    } 

此外,可以嘗試測試API網關通過測試GET方法執行API網關控制檯上。如果仍有問題,請發佈測試調用的「日誌」輸出。

+0

感謝您的回覆。是的,就是這樣。我看到你在使用AWS。在這篇文章中,我有一個關於S3的問題,我希望你能回答嗎? http://stackoverflow.com/questions/41404527/how-to-download-image-from-aws-s3-into-imageview/41404657#41404657 – TheQ

1

所以我解決了這個問題。感謝:@MikeD at AWS

如果有人想從 ANDROID - > API GATEWAY - > LAMBDA FUNCTION(終點,返回一個值) - > API GATEWAY - > ANDROID。

  1. 確保您的lambda函數返回某些內容(字符串或數字)。

因此,繼承人一個簡單的計算器lambda函數:

console.log('Loading the Calc function'); 

exports.handler = function(event, context) { 
    console.log('Received event:', JSON.stringify(event, null, 2)); 
    if (event.a === undefined || event.b === undefined || event.op === undefined) { 
     context.fail("400 Invalid Input"); 
    } 
    var res = {}; 
    res.a = Number(event.a); 
    res.b = Number(event.b); 
    res.op = event.op; 

    if (isNaN(event.a) || isNaN(event.b)) { 
     context.fail("400 Invalid Operand"); 
    } 

    switch(event.op) 
    { 
     case "+": 
     case "add": 
      res.c = res.a + res.b; 
      console.log('WORKED FROM ANDROID!'); 
      break; 
     case "-": 
     case "sub": 
      res.c = res.a - res.b; 
      break; 
     case "*": 
     case "mul": 
      res.c = res.a * res.b; 
      break; 
     case "/": 
     case "div": 
      res.c = res.b===0 ? NaN : Number(event.a)/Number(event.b); 
      break; 
     default: 
      context.fail("400 Invalid Operator"); 
      break; 
    } 
    context.succeed(res); 
}; 

需要注意的是,我返回的變量名是res。其中包含3個數字a,b和c。

現在,如果您要從Lambda - > API網關 - > Android返回。在您的集成響應中,在body mapping模板中,在application/json中,我有以下模板:

#set($allParams = $input.params()) 
{ 
    "c" : $input.json('$.c') 


} 

接下來,在您的方法響應中。確保你創建了一個模型(你可以在左邊的窗格上爲你的方法創建模型,如果你點擊「模型」)進行設置。這裏是我的輸出模式:

{ 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "type": "object", 
    "properties": { 
     "c": {"type":"string"} 
    }, 
    "title": "Output" 
} 

我把這個模型方法響應(重複)。並注意我的模型(在方法響應中)c以及我的集成響應中的身體映射模板。如果您願意,您可以輸入:"c": {"type":"number"}

然後當你部署你的API。它應該工作(如果你的錯誤來自路徑:Lambda - > Api網關 - > Android)。

相關問題