When posting a new product, my server needs to catch files with keys. There's no limit to how many files. Files are unlimited.
With Retrofit 1.9, everything worked perfect. After I update to Retrofit 2, my server does not receive any file.
If I edit the server, it won't be backward compatible anymore. I need to make the android app works like it worked with Retrofit 1.9.
Here's how I implemented.
Retrofit 1.9
class to create ApiService interface.
public class ApiClient {
public interface ApiInterface {
@Multipart
@POST("/products/")
void uploadProduct(
@PartMap Map<String, String> params,
@PartMap Map<String, TypedFile> files,
Callback<Product> cb);
}
}
using the ApiService.
Map<String, String> params = new HashMap<>();
params.put("title", title);
params.put("price", price);
params.put("content", content);
Map<String, TypedFile> files = new HashMap<>();
for (int pos = 0; pos < photoPaths.length; pos++) {
if (!Strings.isNullOrEmpty(photoPaths[pos])) {
TypedFile typedFile = new TypedFile("multipart/form-data", new File(photoPaths[pos]));
files.put("photo_" + String.valueOf(pos + 1), typedFile);
}
}
apiInterface.uploadProduct(params, files, cb);
Retrofit 2
class to create ApiService interface.
public class ApiClient {
public interface ApiInterface {
@Multipart
@POST("/products/")
Call<Product> uploadProduct(
@PartMap Map<String, RequestBody> params,
@PartMap Map<String, RequestBody> files);
}
public static final String MULTIPART_FORM_DATA = "multipart/form-data";
public static RequestBody createRequestBody(@NonNull File file) {
return RequestBody.create(
MediaType.parse(MULTIPART_FORM_DATA), file);
}
public static RequestBody createRequestBody(@NonNull String s) {
return RequestBody.create(
MediaType.parse(MULTIPART_FORM_DATA), s);
}
}
using the ApiService
Map<String, RequestBody> params = new HashMap<>();
params.put("title", ApiClient.createRequestBody(title));
params.put("price", ApiClient.createRequestBody(price));
params.put("content", ApiClient.createRequestBody(content));
Map<String, RequestBody> files = new HashMap<>();
for (int pos = 0; pos < photoPaths.length; pos++) {
if (!TextUtils.isEmpty(photoPaths[pos])) {
RequestBody requestBody = ApiClient.createRequestBody(new File(photoPaths[pos]));
files.put("photo_" + String.valueOf(pos + 1), requestBody);
}
}
Call<Product> call = apiInterface.uploadProduct(params, files);