Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to upload ArrayList of objects inside object there is image file ? #3853

Open
kajal452 opened this issue Feb 14, 2023 · 1 comment
Open

Comments

@kajal452
Copy link

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 ?

@tomridder
Copy link

public class EmployeeRequest {
private String name;
private String phone;
private String companyName;
private String abn;
private List 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 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 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 call = apiService.createEmployee(employeeRequest);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants