Skip to content

Instantly share code, notes, and snippets.

@FAQEnD
Last active March 13, 2017 12:58
Show Gist options
  • Save FAQEnD/efae329cd3cd93be7fb7fdef769cad9c to your computer and use it in GitHub Desktop.
Save FAQEnD/efae329cd3cd93be7fb7fdef769cad9c to your computer and use it in GitHub Desktop.
public class NetworkOperations {
@Override
public Response<ResponseBody> directUpload(File file, String fileType, String validationKey) throws IOException {
Logger.debug(TAG, "Type of file: " + fileType);
Logger.info(TAG, "Uploading of file with name: " + file.getName());
FileDataRequest wrappedFileData = WrapUtils.wrapUploadFileData(file); // any additional metadata
MultipartBody.Part rawFile = WrapUtils.wrapFile(file, fileType); // wrap raw file into multipart body
Call<UploadedFileResponse> uploadCall = mService.directUpload(wrappedFileData, rawFile, validationKey);
Response<UploadedFileResponse> response = uploadCall.execute();
// handling of response
}
}
public interface SomeRetrofitService {
@Multipart
@POST("/upload?action=save")
Call<UploadedFileResponse> directUpload(@Part("data") FileDataRequest data, @Part MultipartBody.Part rawFile, @Query("validationkey") String validationKey);
}
public class WrapUtils {
private static final String MEDIA_TYPE_MULTIPART_FORM_DATA = "multipart/form-data";
public static MultipartBody.Part wrapFile(File file, String fileType) {
RequestBody requestFile = new RequestBody(file, MediaType.parse(MEDIA_TYPE_MULTIPART_FORM_DATA));
return MultipartBody.Part.createFormData(fileType, file.getName(), requestFile);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment