{
"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数组。任何人都可以告诉如何在retrofit中传递此请求。以上是我要通过的json请求在改造体中
答案 0 :(得分:1)
使用此模型类构建请求主体,您应该根据需要使用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);
答案 1 :(得分:0)
首先从你请求结果中创建一个JSONObject。 例如:
JSONObject jObject = new JSONObject(string request result)
然后从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) {}
答案 2 :(得分:0)
创建表示json(POJO)的模型对象。改造将使用Gson进行序列化。
使用正文表示法在改造界面中定义Post方法:MyModule::Utils::Slug.slug_it(attrs[:name], true)
答案 3 :(得分:0)
像这样创建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
答案 4 :(得分:-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 { private String payment_method;
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);