27

How can I post following parameter in retrofit through post method ?

 "params":{"body": {
    "learning_objective_uuids": [
      "ED4FE2BB2008FDA9C8133FF462959C0968FAB98C4D1DB8F2"
    ],
    "note": "FasfAFSASFASDF",
    "user_uuids": [
      "EDF8B7EC20005ACC5C40FF7D6E988801F5BAD83CBBCDB97F",
      "EDF8F78F2000569C64101F244AA20C0070D2A7FCB1939E19"
    ]
  }
}
} }
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Deepak Sharma
  • 4,999
  • 5
  • 51
  • 61

7 Answers7

62
@FormUrlEncoded
@POST("service_name") 
   void functionName(
        @FieldMap Map<String, String> learning_objective_uuids, @FieldMap Map<String, String> user_uuids, @Field("note") String note,
        Callback<CallBackClass> callback
    );

Better solution : Use arraylist.. Reference link : johnsonsu

@FormUrlEncoded
    @POST("service_name") 
       void functionName(
            @Field("learning_objective_uuids[]") ArrayList<String> learning_objective_uuids, @Field("user_uuids[]") ArrayList<String> user_uuids, @Field("note") String note,
            Callback<CallBackClass> callback
        );
hkaraoglu
  • 1,345
  • 1
  • 15
  • 34
8

see this example where i need to pass registration fields data as json request

@POST("magento2apidemo/rest/V1/customers")
Call<RegisterEntity> customerRegistration(@Body JsonObject registrationData);

here i have created registrationData is

private static JsonObject generateRegistrationRequest() {
        JSONObject jsonObject = new JSONObject();
        try {
            JSONObject subJsonObject = new JSONObject();
            subJsonObject.put("email", "abc@xyz.com");
            subJsonObject.put("firstname", "abc");
            subJsonObject.put("lastname", "xyz");

            jsonObject.put("customer", subJsonObject);
            jsonObject.put("password", "password");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonParser jsonParser = new JsonParser();
        JsonObject gsonObject = (JsonObject) jsonParser.parse(jsonObject.toString());
        return gsonObject;
    }
Divyang Panchal
  • 1,889
  • 1
  • 19
  • 27
7

As of today, running the Retrofit implementation 'com.squareup.retrofit2:retrofit:2.1.0'

This works perfectly...

@FormUrlEncoded
@POST("index.php?action=item")
Call<Reply> updateManyItem(@Header("Authorization") String auth_token, @Field("items[]") List<Integer> items, @Field("method") String method);

You can disregard the @Header and @Field("method") .... the main piece is @Field("items[]") List<Integer> items

This is what allows you to send the items. On the API side I am simply looking for an array of integers and this works perfectly.

Goddard
  • 2,863
  • 31
  • 37
2

Go to this site : JSON Schema 2 POJO

Paste your example Json format and then

Select source type : JSON , annotation style : None

Create a POJO class then , for example your class name : MyPOJOClass

Then in your Api :

@POST("endpoint")
public Call<Void> postArray(@Body MyPOJOClass mypojoclass);

If you have headers too you can add them in parameters like that :

@Header("Accept") String accept,@Header("Content-Type") String contentType

@Edit : for your comment checkout my answer : how-to-use-gson-2-0-on-onresponse-from-retrofit-2-0

Community
  • 1
  • 1
Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58
  • Currently I am using this method:- @POST(Constants.POST_UPLOAD_NOTES) public void postNotes(@Body JSONArray jsonArray ,@Query("note") String note, Callback callback); } – Deepak Sharma May 16 '16 at 12:55
  • checkout my @Edit section, you can find an example request and how to handle response .You can combine both answers – Yasin Kaçmaz May 16 '16 at 12:57
1

I've found a new workaround:

you can send it as a String:

@POST("CollectionPoints")
@FormUrlEncoded
Call<SomeResponse> postSomething(@Field("ids")String ids);

and send pass it like this:

Call<SomeResponse> call = service.postSomething("0","0", Arrays.toString(new int[]{53551, 53554}));

Best Regards!

Sebastian Corradi
  • 1,353
  • 2
  • 14
  • 25
1

Gson is the Best solution for JSON Object/Array related problems.

Here, I am sharing my easiest solution for passing array type value in retrofit API

id: ArrayList<String> //Already initilized 
status: String  //Already initilized 

val jsonObject = JsonObject()
val toJson = Gson().toJsonTree(id) //Only one line to covert array JsonElement

jsonObject.add("id", toJson) //Add Json Element in JsonObject

jsonObject.addProperty("status", status)

API Calling using jsonObject

@POST("API_END_POINT")
fun changeStatusOfList(@Body jsonObject: JsonObject): Observable<Response<RETURN_TYPE>>

Output in Log:

{"id":["426","427"],"status":"1"}
Rahul
  • 579
  • 1
  • 8
  • 18
-1

if you want to send a list of the same name the only thing that worked for me in retrofit2 is to use @Query

@FormUrlEncoded
@POST("service_name") 
   void functionName(
        @Query("category") List<Int> categories
    );

this will send it like: https://example.com/things?category=100&category=101&category=105

the accepted answers seem not to work in Retrofit2

humazed
  • 74,687
  • 32
  • 99
  • 138