I'm using Spring for Android as a REST template for remote calls in Android app.
Currently working on uploading images to the server.
I came up with something like that:
public Picture uploadPicture(String accessToken, String fileToUpload) throws RestClientException {
RestTemplate rest = new RestTemplate();
FormHttpMessageConverter formConverter = new FormHttpMessageConverter();
formConverter.setCharset(Charset.forName("UTF8"));
CustomGsonHttpMessageConverter jsonConverter = new CustomGsonHttpMessageConverter();
rest.getMessageConverters().add(formConverter);
rest.getMessageConverters().add(jsonConverter);
String uploadUri = AppConfig.ROOT_URL.concat(AppConfig.ADD_PHOTO);
HashMap<String, Object> urlVariables = new HashMap<String, Object>();
urlVariables.put("accessToken", accessToken);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAccept(Collections.singletonList(MediaType.parseMediaType("application/json")));
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.add("picture", new FileSystemResource(fileToUpload));
Picture response = rest.postForObject(uploadUri, parts, Picture.class, urlVariables);
return response;
}
which works OK, but now I'd like to get progress updates from it.
Does anyone know if it's possible and how to do that?
Thanks in advance :)
So I had this same problem and decided to take a look into Spring-Android sources. After a lot of digging I found out what I need to extend. Got some of my inspiration from this link.
ProgressListener
CountingInputStream
ListenerFileSystemResource
SendFileTask
MyService
HttpConnector
And I use ListenerFileSystemResource instead of FileSystemResource and works. Hope this will be helpful for someone in the future, since I didn't found any info on this for Spring framework.
You need to override FormHttpMessageConverter and ResourceHttpMessageConverter :