无法将数组作为正文参数传递:无效的数组

时间:2019-04-01 08:40:15

标签: java retrofit2

此处通过curl发出http请求

curl https://api.stripe.com/v1/checkout/sessions \
  -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
  -H "Stripe-Version: 2019-03-14; checkout_sessions_beta=v1" \
  -d success_url="https://example.com/success" \
  -d cancel_url="https://example.com/cancel" \
  -d payment_method_types[]=card \
  -d line_items[][name]=T-shirt \
  -d line_items[][description]="Comfortable cotton t-shirt" \
  -d line_items[][amount]=1500 \
  -d line_items[][currency]=usd \
  -d line_items[][quantity]=2

这是成功的工作。 现在,我想通过Retrofit 2在Java项目中创建它们。

在我的Java项目中,我使用Retrofit 2创建http请求。

import retrofit2.Response;
import java.util.*;

public class Main {

    public static void main(String[] args) {
        System.out.println("Start request");

        //Map<String, Object> bodyMap = getParams();
        List<String> payment_method_types = new ArrayList<String>();
        payment_method_types.add("card");

        List<String> line_items = new ArrayList<String>();
        JsonArray jsonArray = new JsonArray();
        JsonObject lineItemsJsonObject = new JsonObject();
        lineItemsJsonObject.addProperty("name", "T-shirt");
        lineItemsJsonObject.addProperty("description", "Comfortable cotton t-shirt");
        lineItemsJsonObject.addProperty("amount", 1500);
        lineItemsJsonObject.addProperty("currency", "usd");
        lineItemsJsonObject.addProperty("quantity", 1);
        jsonArray.add(lineItemsJsonObject);
        line_items.add(jsonArray.toString());

        TransportService.checkoutSessions("https://example.com/success", "https://example.com/cancel", payment_method_types, line_items, new DefaultRestClientCallback<Void>() {
            @Override
            public void onSuccess(Response<Void> response) {
                //super.onSuccess(response);
                System.out.println("checkoutSessions: onSuccess: response = " + response);
            }

            @Override
            public void onError(ErrorResponse errorResponse) {
            }
        });
    }


    public static void checkoutSessions(String success_url,
                                        String cancel_url,
                                        List<String> payment_method_types,
                                        List<String> line_items, Callback<Void> callback) {
        StripeRestClient stripeMonitorRestClient = RestClientFactory.createRestClient(StripeRestClient.class);
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Accept", "application/json");
        headerMap.put("Content-Type", "application/x-www-form-urlencoded");
        headerMap.put("Stripe-Version", "2019-03-14");
        headerMap.put("checkout_sessions_beta", "v1");
        headerMap.put("Authorization", "Bearer sk_test_4eC39HqLyjWDarjtT1zdp7dc");
        Call<Void> adMessagesList = stripeMonitorRestClient.checkoutSessions(headerMap, success_url, cancel_url, payment_method_types, line_items);
        // asynchronously
        adMessagesList.enqueue(callback);
    }

    import retrofit2.Call;
    import retrofit2.http.*;

    import java.util.List;
    import java.util.Map;

    public interface StripeRestClient {
        @FormUrlEncoded
        @POST("checkout/sessions")
        Call<Void> checkoutSessions(@HeaderMap Map<String, String> headers,
            @Field("success_url") String success_url,
            @Field("cancel_url") String cancel_url,
            @Field("payment_method_types[]") List<String> payment_method_types,
            @Field("line_items") List<String> line_items);

    }

但是在启动请求时出现错误。 这里翻新2日志:

> Task :run
Start request
Apr 01, 2019 11:39:28 AM okhttp3.internal.platform.Platform log
INFO: --> POST https://api.stripe.com/v1/checkout/sessions http/1.1
Apr 01, 2019 11:39:28 AM okhttp3.internal.platform.Platform log
INFO: Content-Type: application/x-www-form-urlencoded
Apr 01, 2019 11:39:28 AM okhttp3.internal.platform.Platform log
INFO: Content-Length: 306
Apr 01, 2019 11:39:28 AM okhttp3.internal.platform.Platform log
INFO: Authorization: Bearer sk_test_4eC39HqLyjWDarjtT1zdp7dc
Apr 01, 2019 11:39:28 AM okhttp3.internal.platform.Platform log
INFO: Accept: application/json
Apr 01, 2019 11:39:28 AM okhttp3.internal.platform.Platform log
INFO: checkout_sessions_beta: v1
Apr 01, 2019 11:39:28 AM okhttp3.internal.platform.Platform log
INFO: Stripe-Version: 2019-03-14
Apr 01, 2019 11:39:28 AM okhttp3.internal.platform.Platform log
INFO: 
Apr 01, 2019 11:39:28 AM okhttp3.internal.platform.Platform log
INFO: success_url=https%3A%2F%2Fexample.com%2Fsuccess&cancel_url=https%3A%2F%2Fexample.com%2Fcancel&payment_method_types%5B%5D=card&line_items=%5B%7B%22name%22%3A%22T-shirt%22%2C%22description%22%3A%22Comfortable%20cotton%20t-shirt%22%2C%22amount%22%3A1500%2C%22currency%22%3A%22usd%22%2C%22quantity%22%3A1%7D%5D
Apr 01, 2019 11:39:28 AM okhttp3.internal.platform.Platform log
INFO: --> END POST (306-byte body)
Apr 01, 2019 11:39:29 AM okhttp3.internal.platform.Platform log
INFO: <-- 400 Bad Request https://api.stripe.com/v1/checkout/sessions (1282ms)
Apr 01, 2019 11:39:29 AM okhttp3.internal.platform.Platform log
INFO: Server: nginx
Apr 01, 2019 11:39:29 AM okhttp3.internal.platform.Platform log
INFO: Date: Mon, 01 Apr 2019 08:39:31 GMT
Apr 01, 2019 11:39:29 AM okhttp3.internal.platform.Platform log
INFO: Content-Type: application/json
Apr 01, 2019 11:39:29 AM okhttp3.internal.platform.Platform log
INFO: Content-Length: 116
Apr 01, 2019 11:39:29 AM okhttp3.internal.platform.Platform log
INFO: Connection: keep-alive
Apr 01, 2019 11:39:29 AM okhttp3.internal.platform.Platform log
INFO: access-control-allow-credentials: true
Apr 01, 2019 11:39:29 AM okhttp3.internal.platform.Platform log
INFO: access-control-allow-methods: GET, POST, HEAD, OPTIONS, DELETE
Apr 01, 2019 11:39:29 AM okhttp3.internal.platform.Platform log
INFO: access-control-allow-origin: *
Apr 01, 2019 11:39:29 AM okhttp3.internal.platform.Platform log
INFO: access-control-expose-headers: Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required
Apr 01, 2019 11:39:29 AM okhttp3.internal.platform.Platform log
INFO: access-control-max-age: 300
Apr 01, 2019 11:39:29 AM okhttp3.internal.platform.Platform log
INFO: cache-control: no-cache, no-store
Apr 01, 2019 11:39:29 AM okhttp3.internal.platform.Platform log
INFO: request-id: req_sA1AfUFDGzgqDb
Apr 01, 2019 11:39:29 AM okhttp3.internal.platform.Platform log
INFO: stripe-version: 2019-03-14
Apr 01, 2019 11:39:29 AM okhttp3.internal.platform.Platform log
INFO: Strict-Transport-Security: max-age=31556926; includeSubDomains; preload
Apr 01, 2019 11:39:29 AM okhttp3.internal.platform.Platform log
INFO: 
Apr 01, 2019 11:39:29 AM okhttp3.internal.platform.Platform log
INFO: {
  "error": {
    "message": "Invalid array",
    "param": "line_items",
    "type": "invalid_request_error"
  }
}

Apr 01, 2019 11:39:29 AM okhttp3.internal.platform.Platform log
INFO: <-- END HTTP (116-byte body)
checkoutSessions: onError: errorResponse = ErrorResponse{ code = 0, message = 'null'}

我也尝试这样做:

@FormUrlEncoded
    @POST("checkout/sessions")
    Call<Void> checkoutSessions(@HeaderMap Map<String, String> headers,
                                @Field("success_url") String success_url,
                                @Field("cancel_url") String cancel_url,
                                @Field("payment_method_types") List<String> payment_method_types,
                                @Field("line_items") List<String> line_items);

但是我得到了错误:

  "error": {
    "message": "Invalid array",
    "param": "line_items",
    "type": "invalid_request_error"
  }
}

0 个答案:

没有答案