如何将 aws 生成的 Android SDK 与 api 网关一起使用
How to use the aws genarated Android SDK with api gateway
我部署的 AWS API Gateway
有一个端点:
/users
使用 DELETE
方法。我正在尝试使用 generated SDK
从我的 android app
调用此方法。
我已经部署了 API,然后在 API 网关控制台中生成了 SDK,并下载了名为 aws-apigateway-api-sdk-1.0.0
的 zip 文件夹。然后我在文件夹中 运行 'mvn install' 并将生成的 .jar 文件和库复制到我的项目中并添加 gradle 导入(基本上我做了 these instructions 中的所有事情直到点7).
现在我想在我的 android 代码中创建 SDK。在说明中,他们是这样做的:
// Create an instance of your SDK. Here, 'SimpleCalcClient.java' is the compiled java class for the SDK generated by API Gateway.
final SimpleCalcClient client = factory.build(SimpleCalcClient.class);
我的问题是这个 SimpleCalcClient.class
-> 我知道我需要用我自己的值替换它,但是在我生成的目标文件夹中我只找到以下名为 LambdaSimpleProxyClient.class
的 class,这是一个接口。不过这似乎不正确 class:
我仍然尝试将 ApiClientFactory
与此 class 一起使用,现在我的代码如下所示:
public void deleteUser(View view) {
ApiClientFactory factory = new ApiClientFactory();
final LambdaSimpleProxyClient client = factory.build(LambdaSimpleProxyClient.class);
client.usersDelete();
}
LambdaSimpleProxyClass.java
是在我 运行 mvn install
的第 5 步中生成的,其中包含方法 usersDelete()
,这对我来说很有意义,因为它适合我如何调用我的 API 端点并且看起来很有前途,但我不认为这是我应该在这里使用的 class (但我的 zip 文件夹中没有其他 .class
文件由 API Gateway Generate SDK Method
:
生成
/**
*
*
* @return Empty
*/
@com.amazonaws.mobileconnectors.apigateway.annotation.Operation(path = "/users", method = "DELETE")
Empty usersDelete();
我也不明白如何在这里传递我的查询参数username
,但我仍然尝试调用这个方法。然后程序在 client.usersDelete();
处崩溃
那么除了 LambdaSimpleProxyClient.class
我还应该在那里使用什么?在我生成的 zip 文件夹 aws-apigateway-api-sdk-1.0.0
或我的 maven 生成的目标文件夹中没有其他 .class
文件。
2020-12-21 20:59:32.427 5340-5340/de.stuttgart.syzl3000 D/ProfileActivity: deleteAccount: delete Btn clicked
2020-12-21 20:59:32.441 5340-5340/de.stuttgart.syzl3000 D/AndroidRuntime: Shutting down VM
2020-12-21 20:59:32.445 5340-5340/de.stuttgart.syzl3000 E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.stuttgart.syzl3000, PID: 5340
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: com.amazonaws.mobileconnectors.apigateway.ApiClientException: (Service: null; Status Code: 0; Error Code: null; Request ID: null)
at com.amazonaws.mobileconnectors.apigateway.ApiClientHandler.invoke(ApiClientHandler.java:118)
at java.lang.reflect.Proxy.invoke(Proxy.java:913)
at $Proxy1.usersDelete(Unknown Source)
at de.stuttgart.syzl3000.menu.ProfileActivity.deleteAccount(ProfileActivity.java:49)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:102)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90)
at java.net.InetAddress.getAllByName(InetAddress.java:787)
at com.android.okhttp.Dns.lookup(Dns.java:39)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseMessage(HttpURLConnectionImpl.java:534)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseMessage(DelegatingHttpsURLConnection.java:109)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseMessage(Unknown Source:0)
at com.amazonaws.http.UrlHttpClient.createHttpResponse(UrlHttpClient.java:92)
at com.amazonaws.http.UrlHttpClient.execute(UrlHttpClient.java:85)
at com.amazonaws.mobileconnectors.apigateway.ApiClientHandler.invoke(ApiClientHandler.java:109)
at java.lang.reflect.Proxy.invoke(Proxy.java:913)
at $Proxy1.usersDelete(Unknown Source)
at de.stuttgart.syzl3000.menu.ProfileActivity.deleteAccount(ProfileActivity.java:49)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
欢迎任何提示
我不得不再次 运行 我的 API 网关控制台中的“生成 SDK”,因为不知何故我添加的查询参数不在已部署的版本中。再次部署并生成新的 SDK 后,我将解压缩的文件夹 aws-apigateway-api-sdk-1.0.0
移动到我的 app
文件夹中。然后 LambdaSimpleProxyClient.class
在 factory.build(LambdaSimpleProxyClient.class)
调用中被自动识别,我能够将查询参数传递给现在接受传入的字符串参数的 client.deleteUser("<my-username-value>")
方法。
仍然有错误,但我会为此打开一个新问题。
我部署的 AWS API Gateway
有一个端点:
/users
使用 DELETE
方法。我正在尝试使用 generated SDK
从我的 android app
调用此方法。
我已经部署了 API,然后在 API 网关控制台中生成了 SDK,并下载了名为 aws-apigateway-api-sdk-1.0.0
的 zip 文件夹。然后我在文件夹中 运行 'mvn install' 并将生成的 .jar 文件和库复制到我的项目中并添加 gradle 导入(基本上我做了 these instructions 中的所有事情直到点7).
现在我想在我的 android 代码中创建 SDK。在说明中,他们是这样做的:
// Create an instance of your SDK. Here, 'SimpleCalcClient.java' is the compiled java class for the SDK generated by API Gateway.
final SimpleCalcClient client = factory.build(SimpleCalcClient.class);
我的问题是这个 SimpleCalcClient.class
-> 我知道我需要用我自己的值替换它,但是在我生成的目标文件夹中我只找到以下名为 LambdaSimpleProxyClient.class
的 class,这是一个接口。不过这似乎不正确 class:
我仍然尝试将 ApiClientFactory
与此 class 一起使用,现在我的代码如下所示:
public void deleteUser(View view) {
ApiClientFactory factory = new ApiClientFactory();
final LambdaSimpleProxyClient client = factory.build(LambdaSimpleProxyClient.class);
client.usersDelete();
}
LambdaSimpleProxyClass.java
是在我 运行 mvn install
的第 5 步中生成的,其中包含方法 usersDelete()
,这对我来说很有意义,因为它适合我如何调用我的 API 端点并且看起来很有前途,但我不认为这是我应该在这里使用的 class (但我的 zip 文件夹中没有其他 .class
文件由 API Gateway Generate SDK Method
:
/**
*
*
* @return Empty
*/
@com.amazonaws.mobileconnectors.apigateway.annotation.Operation(path = "/users", method = "DELETE")
Empty usersDelete();
我也不明白如何在这里传递我的查询参数username
,但我仍然尝试调用这个方法。然后程序在 client.usersDelete();
那么除了 LambdaSimpleProxyClient.class
我还应该在那里使用什么?在我生成的 zip 文件夹 aws-apigateway-api-sdk-1.0.0
或我的 maven 生成的目标文件夹中没有其他 .class
文件。
2020-12-21 20:59:32.427 5340-5340/de.stuttgart.syzl3000 D/ProfileActivity: deleteAccount: delete Btn clicked
2020-12-21 20:59:32.441 5340-5340/de.stuttgart.syzl3000 D/AndroidRuntime: Shutting down VM
2020-12-21 20:59:32.445 5340-5340/de.stuttgart.syzl3000 E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.stuttgart.syzl3000, PID: 5340
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: com.amazonaws.mobileconnectors.apigateway.ApiClientException: (Service: null; Status Code: 0; Error Code: null; Request ID: null)
at com.amazonaws.mobileconnectors.apigateway.ApiClientHandler.invoke(ApiClientHandler.java:118)
at java.lang.reflect.Proxy.invoke(Proxy.java:913)
at $Proxy1.usersDelete(Unknown Source)
at de.stuttgart.syzl3000.menu.ProfileActivity.deleteAccount(ProfileActivity.java:49)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:102)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90)
at java.net.InetAddress.getAllByName(InetAddress.java:787)
at com.android.okhttp.Dns.lookup(Dns.java:39)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseMessage(HttpURLConnectionImpl.java:534)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseMessage(DelegatingHttpsURLConnection.java:109)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseMessage(Unknown Source:0)
at com.amazonaws.http.UrlHttpClient.createHttpResponse(UrlHttpClient.java:92)
at com.amazonaws.http.UrlHttpClient.execute(UrlHttpClient.java:85)
at com.amazonaws.mobileconnectors.apigateway.ApiClientHandler.invoke(ApiClientHandler.java:109)
at java.lang.reflect.Proxy.invoke(Proxy.java:913)
at $Proxy1.usersDelete(Unknown Source)
at de.stuttgart.syzl3000.menu.ProfileActivity.deleteAccount(ProfileActivity.java:49)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
欢迎任何提示
我不得不再次 运行 我的 API 网关控制台中的“生成 SDK”,因为不知何故我添加的查询参数不在已部署的版本中。再次部署并生成新的 SDK 后,我将解压缩的文件夹 aws-apigateway-api-sdk-1.0.0
移动到我的 app
文件夹中。然后 LambdaSimpleProxyClient.class
在 factory.build(LambdaSimpleProxyClient.class)
调用中被自动识别,我能够将查询参数传递给现在接受传入的字符串参数的 client.deleteUser("<my-username-value>")
方法。
仍然有错误,但我会为此打开一个新问题。