1

I'm trying to mimic this structure to post data to Mailchimp, I'm using volley

{
 "email_address": "[email protected]",
    "status": "subscribed",
    "merge_fields": {
        "FNAME": "testmailchimp",
        "LNAME": "testingit",
     }  
}

I have done this, but I need to add the FNAME and LNAME and I don't know how to do it right

 @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                params.put("email_address","[email protected]");
                params.put("status","unsubscribed");
                //here i want to add the arraylist FNAME and LNAME
                return params;
            }

Update

I solved my problem with this:

final JSONObject jsonBody = new JSONObject("{\"email_address\":\"[email protected]\"," +
                "\"status\":\"unsubscribed\"," +
                "\"merge_fields\":{\"FNAME\":\"test\",\"LNAME\":\"teste\"}}");

So with this I can do the JSON how I want without maps, and then validate it here https://jsonlint.com/.

1 Answer 1

1

It's not an ArrayList it would be a Map. You can do it using this code

HashMap<String,String> merge_fields = new HashMap<>();
merge_fields.put("FNAME","testmailchimp");
merge_fields.put("LNAME","testingit");
String merge_fields_param = new JSONObject(merge_fields).toString();
params.put("merge_fields",merge_fields_param);
Sign up to request clarification or add additional context in comments.

5 Comments

I logged the output but somehow I'm messing the previous steps. This is what I'm getting: {status=subscribed, merge_fields={"FNAME":"testmailchimp","LNAME":"testingit"}, [email protected]} And I need the same structure as in the post
I think what you need is a Json object but the volley params aren't giving you that. Instead they are individual params
if i use postman with raw and i imput what i ask in the question, the first json text , it posts to me correctly , but im trying to mimic that structure inside my app to send it to the mailchimp, but i cant do it
I think you need to use JsonObjectRequest. Look at this link and provide these params through the JsonRequest object
final JSONObject jsonBody = new JSONObject("{\"email_address\":\"[email protected]\"," + "\"status\":\"unsubscribed\"," + "\"merge_fields\":{\"FNAME\":\"test\",\"LNAME\":\"testero\"}}");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.