0

I am trying to do an API call where the body is an array of files as follows:

[
  {
    "name": "string",
    "type": "string",
    "bytes": "string"
  }, {
    "name": "string",
    "type": "string",
    "bytes": "string"
  }
]

How can I send this list of files using Multipart and Retrofit?

Jahir Fiquitiva
  • 1,459
  • 3
  • 23
  • 47

1 Answers1

-1
You can use retrofit to upload the files by using simple JAVA pojos.

If you have simplifed Json so that we can have java pojos defined as below

JSON:
{
  "files":[
  {
    "name": "string",
    "type": "string",
    "bytes": "string"
  }, {
    "name": "string",
    "type": "string",
    "bytes": "string"
  }
]
}

1.Create Pojo classes for the request json . SampleRequest class you can name it as per your convinence

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang.builder.ToStringBuilder;

public class SampleRequest {

@SerializedName("files")
@Expose
private List<File> files = null;

public List<File> getFiles() {
return files;
}

public void setFiles(List<File> files) {
this.files = files;
}

@Override
public String toString() {
return new ToStringBuilder(this).append("files", files).toString();
}

}

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang.builder.ToStringBuilder;

public class File {

@SerializedName("name")
@Expose
private String name;
@SerializedName("type")
@Expose
private String type;
@SerializedName("bytes")
@Expose
private String bytes;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getBytes() {
return bytes;
}

public void setBytes(String bytes) {
this.bytes = bytes;
}

@Override
public String toString() {
return new ToStringBuilder(this).append("name", name).append("type", type).append("bytes", bytes).toString();
}

}


2.Create an interface for adding the end point for the api e.g:


public interface MyApiEndpointInterface {
    // Request method and URL specified in the annotation
    @POST("sample/upload")
    Call<SomeResponse> uploadFiles(@Body  SampleRequest request);
}

3. In your api calling class or you can use it in base to get the retrofit object for calling the API.


public static final String BASE_URL = "http://api.myservice.com/";
Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(BASE_URL)
    .addConverterFactory(GsonConverterFactory.create())
    .build();

        Call call = retrofit.create(MyApiEndpointInterface.class).uploadFiles(requestBody);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, SampleResponse response) {
                //On success you will receive your response
            }

            @Override
            public void onFailure(Call call, Throwable t) {
                //On failure you will receive your failure response
            }
        });
Gkm
  • 96
  • 1
  • 5
  • If you're serializing JSON to upload it via Retrofit (OkHttp internally), it should be faster to use Moshi or any other stream based serializer. – Jan Heinrich Reimer Nov 17 '18 at 00:56
  • in answers I would always try to avoid using big additional libraries like Apache Commons. It doesn't add necessary functionality and potentially leads to conflicts when trying to "copy-paste" the solution. – Jan Heinrich Reimer Nov 17 '18 at 00:59