kehinde hussein
kehinde hussein

Reputation: 11

How to send nested json objects inside a request body

how can i send this request in the body of my volley request, thanks in advance

{
  "amount": "000",
  "card": {
    "number": "00000",
    "expiry_month": "00",
    "expiry_year": "00",
    "cvv": "000",
    "pin": "000"
  }
}

this is my request parameters, i have try this the api is tell me invalid parametes, please guzs help me

 @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Authorization", User_Token);
            headers.put("Content-Type", "application/json");
            return headers;
        }

        @Override
        public String getBodyContentType() {
            return "application/json";
        }



        @Override
        public byte[] getBody() {
            Map<String, String> params = new HashMap<String, String>();
            params.put( "amount", amount);
            params.put( "card", String.valueOf(come()));

            return new JSONObject(params).toString().getBytes();
        }
        private byte[] come() {
            Map<String, String> params = new HashMap<String, String>();
            params.put( "number", number);
            params.put( "expiry_month", month);
            params.put( "expiry_year", year);
            params.put( "cvv", cvv);
            params.put( "pin", pin);
            return new JSONObject(params).toString().getBytes();
        }

Upvotes: 0

Views: 5641

Answers (2)

j2ko
j2ko

Reputation: 2539

In case you need exact format you need to make sure that values of number,month etc, are passed as strings but not integers. Also to make code look shorter and less destructive you could chain put calls. Here is a bit refactored version:

@Override
public byte[] getBody() {  
    try {
        final JSONObject card = new JSONObject()
                .put("number", String.valueOf(number))
                .put("expiry_month", String.valueOf(month))
                .put("expiry_year", String.valueOf(year))
                .put("cvv", String.valueOf(cvv))
                .put("pin", String.valueOf(pin));

        return new JSONObject()
                .put("amount", String.valueOf(amount))
                .put("card", card)
                .toString()
                .getBytes();    
    } catch (JSONException e) {
        e.printStackTrace();
    }
    //Empty json return will not help us as in this case 
    //we will send useless empty body to the server. 
    return null;
}

I don't know what on caller side of getBody() is happening and whether or not you are able to modify that code. But you need somehow to track that null return or instead introduce your own exception class that should be checked on getBody() caller side. But in case you have no control over getBody() caller side you need return some non null, empty return:

    return new byte[0];

Upvotes: 0

spuente
spuente

Reputation: 324

Use JsonObject instead of: creating a Map, converting it to Byte[] and then getting its String.valueOf(...). This will allow you to send your complete object as body of the request, instead of the incomplete body that is being sent right now: {"amount":"000","card":"[B@a2e...."})

The problem with the value that is being sent for "card": "[B@a2e...." is that it's not a representation of your object with its properties. Instead, it is only the memory address of the Byte Array you created.

In your code, use JsonObject for your objects and only do the conversion to Byte[] at the end of getBody() method, since that is the place where you finally return your complete object:

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Authorization", User_Token);
    // headers.put("Content-Type", "application/json");  // This is probably redundant, as you are already setting it on getBodyContentType()
    return headers;
}

@Override
public String getBodyContentType() {
    return "application/json";
}

@Override
public byte[] getBody() {
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("amount", amount);
        jsonObject.put("card", come());
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return jsonObject.toString().getBytes();
}

private JSONObject come() throws JSONException {
    JSONObject params = new JSONObject();
    params.put( "number", number);
    params.put( "expiry_month", month);
    params.put( "expiry_year", year);
    params.put( "cvv", cvv);
    params.put( "pin", pin);
    return params;
}

Upvotes: 1

Related Questions