【问题标题】:How to pass JSON array inside body in retrofit如何在改造中在正文中传递 JSON 数组
【发布时间】:2017-05-23 10:28:50
【问题描述】:
 {
  "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 请求在改造中

【问题讨论】:

  • 我遇到了正在使用的事务数组的问题。如何在改造中传递这个事务数组?是的,仅在发布请求中

标签: android json paypal retrofit


【解决方案1】:
public class MyPayment {

private String intent;
private MyRedirect redirect_urls;
private MyPayer payer;
private List<MyTrans> transactions;

public MyPayment(String intent, MyRedirect redirect_urls, MyPayer payer, List<MyTrans> transactions) {
    this.intent = intent;
    this.redirect_urls = redirect_urls;
    this.payer = payer;
    this.transactions = transactions;
}

}

公共类 MyRedirect {

private String return_url;
private String cancel_url;

public MyRedirect(String return_url, String cancel_url) {
    this.return_url = return_url;
    this.cancel_url = cancel_url;
}

}

公共类 MyPayer { 私有字符串支付方式;

public MyPayer(String payment_method) {
    this.payment_method = payment_method;
}

}

公共类 MyTrans { 私人 MyAmount 金额;

public MyTrans(MyAmount amount) {
    this.amount = amount;
}

}

公共类 MyAmount {

private String total;
private String currency;

public MyAmount(String total, String currency) {
    this.total = total;
    this.currency = currency;
}

}

    MyRedirect redi = new MyRedirect("http://example.com/your_redirect_url.html","http://example.com/your_cancel_url.html");
    MyPayer mypay = new MyPayer("paypal");
    List<MyTrans> transs = new ArrayList<MyTrans>();


    MyAmount myamo = new MyAmount("6.70","USD");
    MyTrans transact = new MyTrans(myamo);
    transs.add(transact);
    MyPayment mypayment = new MyPayment("sale",redi,mypay,transs);

【讨论】:

  • 以上是我的正确答案,这解决了我的问题
【解决方案2】:

像这样创建 POJO 类:-

 public class PaypalObject {
        @SerializedName("transactions")
        @Expose
        private ArrayList<Transaction> transaction;
    }
   public class Transaction {
        @SerializedName("amount")
        @Expose
        private Amount amount;
    }

    public class Amount {
        @SerializedName("total")
        @Expose
        private String total;

        @SerializedName("currency")
        @Expose
        private String currency;
    }

并将 PaypalObject 作为 @Body PaypalObject paypalObject 传递

【讨论】:

    【解决方案3】:

    使用这个模型类来构建请求体,你应该根据你的需要使用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);
    

    【讨论】:

      【解决方案4】:

      首先根据您的请求结果创建一个 JSONObject。 例如:

         JSONObject jObject = new JSONObject(string request result)
      

      然后使用 from jObject 通过 JSONArray ,getString , getInt 和 ....解析数据。

      看这里:Android, Parsing JSON object


      编辑:设置为 POJO:

      例如你的 POJO 是:

            public class Information {
               public String intent;
               public String return_url;
               public String cancel_url;
            }
      

      并将json data 设置为您的 POJO:

             try {
                 JSONObject jObject = new JSONObject(string request result)
      
                 Information info=new Information;
                 info.intent=jObject.getString("intent");
      
                 JSONObject jObject = new JSONObject(jObject.getString("redirect_urls"))
                 info.return_url=jObject.getString("return_url");
                 info.cancel_url=jObject.getString("cancel_url");
      
             } catch (JSONException e) {}
      

      【讨论】:

      • 但是我在传递请求时使用 pojo 类。你能用 pojo 解释一下吗?
      【解决方案5】:

      创建代表 json (POJO) 的模型对象。改造将使用 Gson 进行序列化。 在改造接口中使用正文符号定义 Post 方法:@Body RequestBody params

      【讨论】:

      • 我想你已经接近答案了。你能用代码解释一下吗?
      • 你有一些创建 pojo 的例子。只需将此对象传递给改造的 POST 方法(而不是 json 字符串)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      相关资源
      最近更新 更多