如何在 Android 的 AWS 云逻辑中 POST

How to POST in AWS cloud logic for Android

因为我正在使用 AWS Mobile hub 进行云逻辑集成。

我如何将数据发送到 lambda 函数?

代码:

public void postCloudLogic(String mName,String mClass) {
    // Create components of api request
    final String method = "POST";

    final String path = "/test_rds_lambda/?name="+mName+"&class="+mClass;

    final String body = "";
    final byte[] content = body.getBytes(StringUtils.UTF8);

    final Map parameters = new HashMap<>();
    parameters.put("lang", "en_US");

    final Map headers = new HashMap<>();

    // Use components to create the api request
    ApiRequest localRequest =
            new ApiRequest(apiClient.getClass().getSimpleName())
                    .withPath(path)
                    .withHttpMethod(HttpMethodName.valueOf(method))
                    .withHeaders(headers)
                    .addHeader("Content-Type", "application/json")
                    .withParameters(parameters);
    ...

如你所见,我使用了:

final String path = "/test_rds_lambda/?name="+mName+"&class="+mClass;

这是我从中得到的错误:

{message=No method found matching route test_rds_lambda/%3Fname%3DYoME%26class%3DYoClass for http method POST.}

我的请求 URL 路径有

?,=

等,但它们已更改为十六进制。即

%3Fname%3D

在 aws 控制台的 "test api" 中如何防止这种情况发生。

Post 参数应在请求正文中发送。

这里是aws-sdk-android上的方法: https://github.com/aws/aws-sdk-android/blob/master/aws-android-sdk-apigateway-core/src/main/java/com/amazonaws/mobileconnectors/apigateway/ApiRequest.java#L163

所以,我通过将数据放入 JSONObject 然后将其传递到云逻辑的 body 参数中解决了这个问题。

public void callCloudLogic() throws JSONException {
    // Create components of api request
    final String method = "POST";

    final String path = "/lambda_func";

    *--> JSONObject data =new JSONObject();
    *--> data.put("mCase","1");
    *--> final String body = data.toString();
    *--> final byte[] content = body.getBytes(StringUtils.UTF8);

    final Map parameters = new HashMap<>();
    parameters.put("lang", "en_US");

    final Map headers = new HashMap<>();

    // Use components to create the api request
    ApiRequest localRequest =
            new ApiRequest(apiClient.getClass().getSimpleName())
                    .withPath(path)
                    .withHttpMethod(HttpMethodName.valueOf(method))
                    .withHeaders(headers)
                    .addHeader("Content-Type", "application/json")
                    ;