android - AWS API Gateway SDK,Android 问题
问题描述
在过去的两天里,我一直在尝试使用 AWS API Gateway 生成的 Android SDK,但没有取得多大成功。我按照教程使用 Lambda 函数创建了一个简单计算器,创建了一个简单计算器 API 并将两者集成。
使用 API 控制台,我可以测试 POST 并且它工作正常,我收到了提供两个数字和一个操作的正确结果。当我为 Android 生成 SDK 时,我的问题就开始了。
按照本教程(https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-generate-sdk-android.html)我创建了.jar文件并将它们添加到“libs”文件夹中Android Studio 以及依赖项。
但是,当我调用“client.rootPost()”方法时出现错误。在不复制整个错误日志的情况下,错误显示为:
at com.amazonaws.mobileconnectors.apigateway.ApiClientHandler.invoke(ApiClientHandler.java:118)
at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
at $Proxy3.execute(Unknown Source)
at com.example.awstest.MainActivity.onCreate(MainActivity.java:52)
我在 MainActivity 中的代码如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
ApiClientFactory factory = new ApiClientFactory();
final SimpleCalculatorAPIClient client = factory.build(SimpleCalculatorAPIClient.class);
Input body = new Input();
body.setA(new BigDecimal(1));
body.setB(new BigDecimal(1));
body.setOp("+");
Result output = client.rootPost(body); //LINE 52, where the error points to
String result = output.getOutput().getC().toString();
}
在我的 build.gradle 中,我有以下依赖项:
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation fileTree(include: ['*.jar'], dir: 'app/libs')
implementation 'com.amazonaws:aws-android-sdk-core:2.16.11'
implementation 'com.amazonaws:aws-android-sdk-apigateway-core:2.16.11'
最后两个依赖项不是本教程的一部分,但我发现我需要它们才能从此处发布的另一个问题中调用“ApiClientFactory”类。这让我相信我错过了一些非常明显的东西。仅仅遵循上面的教程是不够的,它假设你已经知道一些事情,而我不知道。
任何想法我在这里想念什么?
解决方案
我发现了我的问题。
我收到错误是因为代码在“onCreate”上运行,而不是它必须在它自己的线程中运行。例子:
private void awsTest() {
Thread thread = new Thread() {
@Override
public void run() {
try {
sleep(10);
ApiClientFactory factory = new ApiClientFactory();
final SimpleCalculatorAPIClient client = factory.build(SimpleCalculatorAPIClient.class);
Input body = new Input();
body.setA(new BigDecimal(1));
body.setB(new BigDecimal(1));
body.setOp("+");
Output output = client.rootPost(body);
Result result = new Result();
result.setOutput(output);
String i = result.getOutput().getC().toPlainString();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}
推荐阅读
- c# - 启动文本随机化它,它不会让我发布因为名称
- sql - 错误:使用计数和查找百分比时在“从”处或附近出现语法错误
- r - 如何基于 R 中的多列展开单列?
- apache-kafka - 消费端的并行处理和维护秩序 - Kafka + Camel
- node.js - 使用 CloudFront 和 FFMPEG 通过 HTTP 进行实时流式传输存在延迟问题
- trace - 如何理解 zipkin 中的 span 标签 peer.address?
- php - 如何通过 Ajax 检索记录并在 Laravel 表中显示
- python - scikit-learn 中简单神经网络实现对数据集大小的要求过高
- ruby-on-rails - 渐进式 Web 应用程序应该使用 simple_token_authentication 还是 jwt 令牌?
- tensorflow - amazon sagemaker 上的 base64 编码图像推断