Is it possible to POST a package of json objects?

一笑奈何 提交于 2019-12-24 07:37:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!