Search code examples
androidarraysjsonretrofit

Sending array value as parameter retrofit android


I need to send a parameter as array in retrofit. This is the required key-value:

 "country":["Canada","India"]

Now, when i pass it, It changes the whole array into string. This is how i am passing the value:

"country":"["Canada", "India"]"

The code i am using is here:

ArrayList<String> countriedAplied = new ArrayList<>();

JSONObject jsonObject = new JSONObject();
try
{
  jsonObject.put("country", countriedAplied);
}

Solution

  •  ArrayList<String> countriedAplied = new ArrayList<>();
    
            JSONArray jsonArray = new JSONArray();
            JSONObject jsonObject=new JSONObject();
    
                jsonArray.put(countriedAplied);
            try {
                jsonObject.put("Country",jsonArray);
            } catch (JSONException e) {
                e.printStackTrace();
            }
    
    JsonParser jsonParser = new JsonParser();
    JsonObject gsonObject = new JsonObject();
    gsonObject = (JsonObject) jsonParser.parse(jsonObject.toString());
    sendtoDB(gsonObject);