java - Retrofit - Multipart Form with image upload and string array -
i creating android app using retrofit 2.0 send post request server. request allow user create group has name, array of users, , image. using multipart form in request because app uploads image server in form of file.
this presenter, given fields needed request.
public void submitgroup(@nonnull final string accesstoken, @nonnull final string userid, @nonnull final string groupname, @nonnull final string groupiconuri, @nullable final string[] members) { file file = new file(groupiconuri); requestbody requestowner = requestbody.create( mediatype.parse("multipart/form-data"), userid); requestbody requestgroupname = requestbody.create( mediatype.parse("multipart/form-data"), groupname); /** * requestmembers string[] containing member * id. field having trouble * making requestbody for. **/ requestbody requestmembers = requestbody.create( mediatype.parse("multipart/form-data"), string.valueof(members)); multipartbody.part filepart = multipartbody.part .createformdata("image", file.getname(), requestbody.create( mediatype.parse("image/*"), file)); log.e(tag, "accesstoken: " + accesstoken); log.e(tag, "userid: " + userid); log.e(tag, "groupname: " + groupname); log.e(tag, "groupiconuri: " + groupiconuri); // log.e(tag, "members: " + members); call<group> call = service.creategroup(accesstoken, requestgroupname, requestowner, filepart); call.enqueue(this); }
here application service class, have run , has worked without including string[] field members.
public interface service { @multipart @post("/group") call<group> creategroup(@header("authorization") string authorization, @part("name") requestbody groupname, @part("owner") requestbody ownerid, @part multipartbody.part file);
here group class json being serialized to:
public class group { @nonnull @serializedname("name") private string name; @nonnull @serializedname("owner") private object owner; @nonnull @serializedname("image") private string image_name; @nonnull @serializedname("pin") private int pin; @nonnull @serializedname("created_at") private date created_at; @nullable @serializedname("users") private object[] users; @nullable @serializedname("pending_users") private object[] pending_users; public group(@nonnull final string name, @nonnull final object owner, @nullable final object[] users, @nullable final object[] pending_users, @nonnull final string image_name, @nonnull final date created_at, @nonnull final int pin) { this.name = name; this.owner = owner; this.users = users; this.pending_users = pending_users; this.image_name = image_name; this.created_at = created_at; this.pin = pin; } @nonnull public date getcreated_at() { return created_at; } @nonnull public string getname() { return name; } @nonnull public object getowner() { return owner; } @nonnull public int getpin() { return pin; } public object getusers() { return users; } public object getpending_users() { return pending_users; } public string getimage_name() { return image_name; }
my questions are:
- how can create send member string[] requestbody? have read can hashmapped, way?
- is necessary have groupname , ownerid typecasted requestbody? can send @part string?
Comments
Post a Comment