1

so I am building an applications that has to POST a json array with some information and also json object with user credentials which are going to be used for identification. Is is possible to POST some kind of package that contains json object array + object with user credentials? I am using Retrofit2.

So beside this Array list I would like to send Credentials with one POST request.

public interface JsonPlaceHolderApi {

    @POST("hws/hibisWsTemplate/api/v1/order/posts/")
    Call<Post> createPost(@Body ArrayList<Post> post);
}
Bruno
  • 3,872
  • 4
  • 20
  • 37
Tomaz Mazej
  • 465
  • 5
  • 19

2 Answers2

2

You have to do something like that

Define your API

public interface JsonPlaceHolderApi {

  @POST("hws/hibisWsTemplate/api/v1/order/posts/")
  Call<Post> createPost(@Body PostRequest post);
}

Define your request

public class PostRequest {
  final ArrayList<Post> posts;
  final String credentials; // or anything you want

  PostRequest(ArrayList<Post> posts, String credentials) {
    this.posts = posts;
    this.credentials = credentials;
  }
}
Bruno
  • 3,872
  • 4
  • 20
  • 37
1

You have to create a class for credentials just like you made a class for your array. Then you create another class named lets say "Request" and put credentials and your array in it like so:

public class Request {
    final ArrayList<Post> posts;
    final Credentials credentials;

    //constructor, getters/setters
    ...

and then in your api do this:

public interface JsonPlaceHolderApi {

    @POST("hws/hibisWsTemplate/api/v1/order/posts/")
   Call<Post> createPost(@Body Request post);
}
Azy
  • 61
  • 6