JsonReaderException,从Android调用AWS lambda函数时

时间:2018-09-27 05:51:46

标签: java android amazon-web-services aws-lambda

嗨,我正在尝试从移动后端调用lambda函数。

我已经使用.Net Core生成了Lambda函数

      public string FunctionHandler(string text, ILambdaContext 
      {
         return qrcode;
      }

此调用很好,.net core和AWS控制台中没有任何错误。

但是当我尝试从Android Studio调用它时,出现以下错误。

{
"errorType": "JsonReaderException",
"errorMessage": "Unexpected character encountered while parsing value: {. Path '', line 1, position 1.",
"stackTrace": [
"at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)",
"at Newtonsoft.Json.JsonTextReader.ReadAsString()",
"at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)",
"at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)",
"at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)",
"at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)",
"at lambda_method(Closure , Stream , Stream , ContextInfo )"
]
}

Java代码

CognitoCachingCredentialsProvider cognitoProvider = new CognitoCachingCredentialsProvider(MainActivity.this, "xxxxx", xxxxx);

LambdaInvokerFactory factory = new LambdaInvokerFactory(MainActivity.this,
                                xxxxx, cognitoProvider);

final MyInterface myInterface = factory.build(MyInterface.class);

RequestClass request = new RequestClass("Hello World");

new AsyncTask<RequestClass, Void, ResponseClass>() {@Override protected ResponseClass doInBackground(RequestClass... params) 
{

 try {
     return myInterface.TestApp(params[0]);
 } catch (LambdaFunctionException lfe) {
     Log.e("Tag", "Failed to invoke echo", lfe);
     return null;
 }
 catch(Exception e){
 Log.e("Tag", "Failed to invoke echo", e);
 return null;
 }
 }

接口:

import com.amazonaws.mobileconnectors.lambdainvoker.LambdaFunction;
public interface MyInterface {
    @LambdaFunction(functionName="TestApp")
    ResponseClass TestApp(RequestClass request);

}

1 个答案:

答案 0 :(得分:0)

当您的lambda函数接受一个字符串参数并返回一个字符串时,该函数的接口应类似于:

公共接口MyInterface {

//Instantiate serializer
var serializer = new DynamicTableEntityJsonSerializer.DynamicTableEntityJsonSerializer();

//Serialize DynamicTableEntity into Json string
var serializedEntity = serializer.Serialize(dynamicTableEntity);

}