尝试使用生成的开发工具包从 Android 进行"Hello World" AWS API 网关 GET API 调用



我正在尝试让AWS API网关生成的开发工具包工作,我创建了一个API网关GET API,当我从浏览器/httpclient点击url时,它可以正常工作,但不适用于Android。

对于 Android,我正在尝试使用由 AWS API Gateway 生成的 Android SDK。我发现的文档示例对我来说非常不清楚,它使用了 AWS 开发工具包中不存在的类型和方法:

http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-generate-sdk-android.html

例如:

//     If the API exposes a 'GET /{a}/{b}/{op}'  method, you can call the following SDK method to invoke the request,
Result.output = client.aBOpGet(a, b, c)

我不知道aBOpGET应该是什么,也不知道结果是什么。

虽然我能够按照 Jar 来构建/导入 jar,但我无法遵循他们的代码示例。所以,我根据浏览SDK代码想出了这个:

import android.os.AsyncTask;
import android.util.Log;
import com.amazonaws.mobileconnectors.apigateway.ApiClientFactory;
import com.amazonaws.mobileconnectors.apigateway.ApiRequest;
import com.amazonaws.mobileconnectors.apigateway.ApiResponse;
import Client.LambdaMicroserviceClient;

public class APIGatewayHandler extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
//gateway
ApiClientFactory factory = new ApiClientFactory();
final LambdaMicroserviceClient client = factory.build(LambdaMicroserviceClient.class);
ApiRequest request = new ApiRequest("/QueryNetSumMinute");

ApiResponse apiResponse = client.execute(request);
int responseCode = apiResponse.getStatusCode();
android.util.Log.v("Request: ", request.toString());
android.util.Log.v("response Code: ", String.valueOf(responseCode));
android.util.Log.v("response Status Text: ",    apiResponse.getStatusText());
//android.util.Log.v("responseCode: ", String.valueOf(responseCode));
return null;
}
@Override
public void onPostExecute(Void var) {
Log.d("onPostExecute", "complete");
}
}

这运行良好,但在客户端响应中,我在调用时收到 403 禁止错误。

我在 API 网关上启用了信息日志记录,我可以从浏览器发送的 http 请求中看到详细信息,但我从这个 Android 代码中看不到任何东西。

我怀疑正在发生的事情是,客户端在没有指定方法"/QueryNetSumMinute"的情况下访问了 API 的 BASE URL,从浏览器进行测试确实返回 403

让它工作了,以防这有助于任何人看到下面。我必须添加request.getPath("/MyAPIName"(和一些调试代码:

import android.os.AsyncTask;
import android.util.Log;
import com.amazonaws.mobileconnectors.apigateway.ApiClientFactory;
import com.amazonaws.mobileconnectors.apigateway.ApiRequest;
import com.amazonaws.mobileconnectors.apigateway.ApiResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import Client.LambdaMicroserviceClient;

public class APIGatewayHandler extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
//gateway
ApiClientFactory factory = new ApiClientFactory();
final LambdaMicroserviceClient client = factory.build(LambdaMicroserviceClient.class);
ApiRequest request = new ApiRequest();
request.withPath("/QueryNetSumMinute");
ApiResponse apiResponse = client.execute(request);
int responseCode = apiResponse.getStatusCode();
String responseBody = "empty";
try {
responseBody   = convertStreamToString(apiResponse.getRawContent());
} catch (Exception e){
Log.d("ERROR ", " failed reading response ");
}
android.util.Log.v("Request: ", request.toString());
android.util.Log.v("response Code: ", String.valueOf(responseCode));
android.util.Log.v("response Status Text: ",    apiResponse.getStatusText());
android.util.Log.v("responseBody: ", responseBody);
return null;
}
@Override
public void onPostExecute(Void var) {
Log.d("onPostExecute", "complete");
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}

最新更新