问题
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);
}
回答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);
}
回答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;
}
}
来源:https://stackoverflow.com/questions/57954204/is-it-possible-to-post-a-package-of-json-objects