retrofit
retrofit copied to clipboard
How to upload ArrayList of objects inside object there is image file ?
Arraylist of objects inside object there is file and id how to send this type of request in retrofit 2.0 ?
{ name:Employee one phone:9876543210 companyName:ows abn:ows1234 licenses[0].license[0] file licenses[0].license[1] file licenses[0].id:1 }
how to send above type of request in android retrofit 2 ?
public class EmployeeRequest { private String name; private String phone; private String companyName; private String abn; private List<License> licenses;
public EmployeeRequest(String name, String phone, String companyName, String abn, List<License> licenses) {
this.name = name;
this.phone = phone;
this.companyName = companyName;
this.abn = abn;
this.licenses = licenses;
}
public static class License {
private List<File> license;
private int id;
public License(List<File> license, int id) {
this.license = license;
this.id = id;
}
}
}
public interface MyApiService { @POST("employee") Call<Void> createEmployee(@Body EmployeeRequest employeeRequest); }
// create a Retrofit instance Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://example.com/api/") .build();
// create an instance of the MyApiService interface MyApiService apiService = retrofit.create(MyApiService.class);
// create a list of files List<File> licenseFiles = new ArrayList<>(); licenseFiles.add(new File("path/to/file1")); licenseFiles.add(new File("path/to/file2"));
// create a License object EmployeeRequest.License license = new EmployeeRequest.License(licenseFiles, 1);
// create a list of License objects List<EmployeeRequest.License> licenses = new ArrayList<>(); licenses.add(license);
// create an instance of the EmployeeRequest class EmployeeRequest employeeRequest = new EmployeeRequest("Employee one", "9876543210", "ows", "ows1234", licenses);
// make the API call Call<Void> call = apiService.createEmployee(employeeRequest);