改造调用返回 400,cURL 请求工作正常,语法问题

DIR*_*AVE 7 android square retrofit2

我尝试对 API 端点进行改造调用,但它返回了400 error,但是我的 curl 请求工作得很好。我似乎无法发现错误,有人可以仔细检查我的工作,看看我在哪里犯了错误吗?

有效的 curl 调用:

curl --request POST https://connect.squareupsandbox.com/v2/payments \
    --header "Content-Type: application/json" \
    --header "Authorization: Bearer accesstoken112233" \
    --header "Accept: application/json" \
    --data '{
    "idempotency_key": "ab2a118d-53e2-47c6-88e2-8c48cb09bf9b",
    "amount_money": {
    "amount": 100,
    "currency": "USD"},
    "source_id": "cnon:CBASEITjGLBON1y5od2lsdxSPxQ"}'
Run Code Online (Sandbox Code Playgroud)

我的改造电话:

public interface IMakePayment {

        @Headers({
                "Accept: application/json",
                "Content-Type: application/json",
                "Authorization: Bearer accesstoken112233"
        })
        @POST(".")
        Call<Void> listRepos(@Body DataDto dataDto);
    }
Run Code Online (Sandbox Code Playgroud)

DataDto 类:

public class DataDto {

    private String idempotency_key;
    private String amount_money;
    private String source_id;

    public DataDto(String idempotency_key, String amount_money, String source_id) {
        this.idempotency_key = idempotency_key;
        this.amount_money = amount_money;
        this.source_id = source_id;
    }
}
Run Code Online (Sandbox Code Playgroud)

最后进行改造调用:

 DataDto dataDto = new DataDto("ab2a118d-53e2-47c6-88e2-8c48cb09bf9b", "{\"amount\": 100, \"currency\": \"USD\"}", "cnon:CBASEITjGLBON1y5od2lsdxSPxQ");

        RetrofitInterfaces.IMakePayment service = RetrofitClientInstance.getRetrofitInstance().create(RetrofitInterfaces.IMakePayment.class);
        Call<Void> call = service.listRepos(dataDto);
        call.enqueue(new Callback<Void>() {
            @Override
            public void onResponse(@NonNull Call<Void> call, @NonNull Response<Void> response) {
                Log.d(TAG, "onResponse: " + response.toString());
            }

            @Override
            public void onFailure(@NonNull Call<Void> call, @NonNull Throwable t) {
                Log.d(TAG, "onFailure: Error: " + t);
            }
        });
Run Code Online (Sandbox Code Playgroud)

改造实例:

public class RetrofitClientInstance {

    private static Retrofit retrofit;
    private static final String BASE_URL = "https://connect.squareupsandbox.com/v2/payments/";


    public static Retrofit getRetrofitInstance() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑 1:更改为 JSON 对象的第二个参数

JSONObject jsonObject = new JSONObject();
        try{
            jsonObject.put("amount", 100);
            jsonObject.put("currency", "USD");
        }catch (Exception e){
            Log.d(TAG, "onCreate: " + e);
        }
        DataDto dataDto = new DataDto("ab2a118d-53e2-47c6-88e2-8c48cb09bf9b", jsonObject, "cnon:CBASEITjGLBON1y5od2lsdxSPxQ");
Run Code Online (Sandbox Code Playgroud)

k_o*_*_o_ 0

传递的 JSON 结构很可能没有以相同的格式序列化。

"amount_money": {
    "amount": 100,
    "currency": "USD"},
Run Code Online (Sandbox Code Playgroud)

我首先会使用具有和字段private String amount_money;的真正的 DTO 。这应该会取得进展。我不是 100% 确定属性的下划线映射是什么样子,但这是下一步。amountcurrency

添加日志记录以便能够查看传递的数据。快速搜索即可发现本教程: https: //futurestud.io/tutorials/retrofit-2-log-requests-and-responses。当看到传输的数据时,应该很容易比较预期的数据和发送的数据。