Skip to content Skip to sidebar Skip to footer

How To Pass Json Array Inside Body In Retrofit

{ 'intent':'sale', 'redirect_urls':{ 'return_url':'http://example.com/your_redirect_url.html', 'cancel_url':'http://example.com/your_cancel_url.html' }, 'payer':{

Solution 1:

Use this model class for building the request body, you should use getters and setters as per your need.

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

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

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

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

public static classTransactions{
    @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=newGson();
StringmodelClass=gson.toJson(model);

Solution 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

Solution 3:

at first make a JSONObject from you request result. for example :

JSONObject jObject = newJSONObject(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 :

publicclassInformation{
         publicString intent;
         publicString return_url;
         publicString cancel_url;
      }

and for set json data to your POJO :

try {
           JSONObject jObject = newJSONObject(string request result)

           Information info=newInformation;
           info.intent=jObject.getString("intent");

           JSONObject jObject = newJSONObject(jObject.getString("redirect_urls"))
           info.return_url=jObject.getString("return_url");
           info.cancel_url=jObject.getString("cancel_url");

       } catch (JSONException e) {}

Solution 4:

Create POJO class like this:-

publicclassPaypalObject {
        @SerializedName("transactions")
        @Expose
        private ArrayList<Transaction> transaction;
    }
   publicclassTransaction {
        @SerializedName("amount")
        @Expose
        private Amount amount;
    }

    publicclassAmount {
        @SerializedName("total")
        @Expose
        private String total;

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

And pass the PaypalObject as @Body PaypalObject paypalObject

Solution 5:

publicclassMyPayment{

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 {

privateString return_url;
privateString cancel_url;

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

}

public class MyPayer { private String payment_method;

publicMyPayer(String payment_method){
    this.payment_method = payment_method;
}

}

public class MyTrans { private MyAmount amount;

publicMyTrans(MyAmount amount){
    this.amount = amount;
}

}

public class MyAmount {

privateString total;
privateString currency;

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

}

MyRedirectredi=newMyRedirect("http://example.com/your_redirect_url.html","http://example.com/your_cancel_url.html");
    MyPayermypay=newMyPayer("paypal");
    List<MyTrans> transs = newArrayList<MyTrans>();


    MyAmountmyamo=newMyAmount("6.70","USD");
    MyTranstransact=newMyTrans(myamo);
    transs.add(transact);
    MyPaymentmypayment=newMyPayment("sale",redi,mypay,transs);

Post a Comment for "How To Pass Json Array Inside Body In Retrofit"