所以我有這個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和希望你們能幫助我了我在想念着什麼!
我的方法響應
其次是我的輸出模式
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"username": {"type":"string"}
},
"title": "Output"
}
這裏是我得到的輸出,當我在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函數被調用並執行。但是,我沒有得到回報價值。所以我的方法響應和/或集成響應有些問題,但我不確定是什麼。
感謝您的回覆。是的,就是這樣。我看到你在使用AWS。在這篇文章中,我有一個關於S3的問題,我希望你能回答嗎? http://stackoverflow.com/questions/41404527/how-to-download-image-from-aws-s3-into-imageview/41404657#41404657 – TheQ