改造 - 具有图像上传和字符串数组的多部分表格

时间:2017-04-07 03:44:17

标签: java android retrofit2 multipartform-data

我正在创建一个使用Retrofit 2.0向服务器发送POST请求的Android应用。此请求将允许用户创建具有名称,用户数组和图像的组。我在此请求中使用多部分表单,因为应用程序以文件的形式将图像上载到服务器。

这是演示者,它给出了此请求所需的字段。

 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);
   /**
   * The requestMembers is a String[] containing all member 
   * by id.  This is the field that I am having trouble 
   * making a 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);


}

这是应用程序服务类,我已经运行了它并且它没有包含成员的String []字段。

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);

以下是json被序列化为的组类:

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;
}

我的问题是:

  1. 如何创建发送成员String []作为RequestBody?我已经读过它可以被哈希映射,但这是唯一的方法吗?
  2. 是否有必要将groupName和ownerId类型化为RequestBody?我可以简单地将@Part作为字符串发送吗?

0 个答案:

没有答案