问题
{
"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"
}
}
]
}
I want to pass following request in the body in retrofit.My problem is with json array which is being used as shown above.Can anyone tell how to pass this request in retrofit.Above is the json request which I want to pass in body of retrofit
回答1:
Use this model class for building the request body, you should use getters and setters as per your need.
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;
}
}
You should also add this dependancy;
compile 'com.google.code.gson:gson:2.8.0'
Then inside your retrofit request;
Call<ResponseBody> PaymentRequest(@Body PaymentModel model);
To view your complete model class that you've built, use;
Gson gson=new Gson();
String modelClass =gson.toJson(model);
回答2:
Create the model object that represent the json (POJO). retrofit will use Gson to do the serializing.
define the Post method in the retrofit interface with the body notation: @Body RequestBody params
回答3:
at first make a JSONObject from you request result. for example :
JSONObject jObject = new JSONObject(string request result)
then use from jObject to parse data with JSONArray ,getString , getInt and ....
look at here : Android, Parsing JSON object
Edit : set to POJO :
for example your POJO is :
public class Information {
public String intent;
public String return_url;
public String cancel_url;
}
and for set json data to your 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) {}
回答4:
Create POJO class like this:-
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;
}
And pass the PaypalObject as @Body PaypalObject paypalObject
回答5:
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;
}
}
public class 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;
}
}
public class MyPayer { private String payment_method;
public MyPayer(String payment_method) {
this.payment_method = payment_method;
}
}
public class MyTrans { private MyAmount amount;
public MyTrans(MyAmount amount) {
this.amount = amount;
}
}
public class 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);
来源:https://stackoverflow.com/questions/41543318/how-to-pass-json-array-inside-body-in-retrofit