android - 如何在改造中将 JSON 数组传递到 body 中

标签 android json paypal retrofit

 {
  "intent":"sale",
  "redirect_urls":{
    "return_url":"http://example.com/your_redirect_url.html",
    "cancel_url":"http://example.com/your_cancel_url.html"
  },
  "payer":{
    "payment_method":"paypal"
  },
  "transactions":[
    {
      "amount":{
        "total":"7.47",
        "currency":"USD"
      }
    }
  ]
}

我想在改造中传递以下请求。我的问题是 json 数组,如上所示使用。谁能告诉我如何在改造中传递这个请求。上面是我想传递的 json 请求在改造 body

最佳答案

使用此模型类构建请求主体,您应该根据需要使用 getter 和 setter。

public class PaymentModel {
@SerializedName("intent")
public String intent;
@SerializedName("redirect_urls")
public RedirectUrls redirectUrls;
@SerializedName("payer")
public Payer payer;
@SerializedName("transactions")
public List<Transactions> transactions;

public static class RedirectUrls {
    @SerializedName("return_url")
    public String returnUrl;
    @SerializedName("cancel_url")
    public String cancelUrl;
}

public static class Payer {
    @SerializedName("payment_method")
    public String paymentMethod;
}

public static class Amount {
    @SerializedName("total")
    public String total;
    @SerializedName("currency")
    public String currency;
}

public static class Transactions {
    @SerializedName("amount")
    public Amount amount;
}
}

你还应该添加这个依赖;

 compile 'com.google.code.gson:gson:2.8.0'

然后在你的改造请求中;

Call<ResponseBody> PaymentRequest(@Body PaymentModel model);

要查看您构建的完整模型类,请使用;

Gson gson=new Gson();
String modelClass =gson.toJson(model);

关于android - 如何在改造中将 JSON 数组传递到 body 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41543318/

相关文章:

javascript - 移动设备的 jQuery click 替代品

Android 小部件配置打开 Activity

.net - 为什么 ServiceStack.Text 不默认日期为 iso8601?

c# - C# 中的 Paypal IPN 监听器问题

paypal - 如何判断我的账户是否激活了 Paypal Web Payments Pro?

java - 使用 InputStream 的 String(bytes[]) 构造函数

javascript - 如何使用 JSON 格式创建特定类型的 Javascript 对象?

java - 读取 CSV 文件并用 Java 编写 Json 文件

php - Paypal 快速支付错误 SetExpressCheckout 失败 : Could not resolve host: api-3t(6)

android - 我怎样才能使 Android 模拟器变砖?