Arran
Apr 28th, 2011, 01:39 AM
Hello,
I've been reading a lot on this forum about POST and resttemplate with spring android. I'm having problems sending a serialized object with postForObjects from my android phone. I'm using the simplexml annotations binding to serialize an object with two strings and a "file".
If I try to send the file as a byte[] the process works except that I only seem to get a reference of the object and not the data itself.
So I tried to send it as a ByteArrayResource (from spring android framework), but then it wont run since it can't find an appropriate message converter.
I'm trying to send it like this
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATIO N_XML);
HttpEntity<Msg> requestEntity = new HttpEntity<Msg>(crapport, requestHeaders);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
String response = responseEntity.getBody();
and my object looks something like this (with appropriate getters/setters)
@Root
public class Msg {
@Element
private String comment;
@Element
private String address;
@Element
ByteArrayResource image;
}
Do I need to use a multipart to send strings combined with an byte array or is there some other way to do it?
I haven't tried excplicitly setting additional messageconverters, and it seems (by looking at the source) that string and bytearray message converters are added as default anyway.
I've been reading a lot on this forum about POST and resttemplate with spring android. I'm having problems sending a serialized object with postForObjects from my android phone. I'm using the simplexml annotations binding to serialize an object with two strings and a "file".
If I try to send the file as a byte[] the process works except that I only seem to get a reference of the object and not the data itself.
So I tried to send it as a ByteArrayResource (from spring android framework), but then it wont run since it can't find an appropriate message converter.
I'm trying to send it like this
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATIO N_XML);
HttpEntity<Msg> requestEntity = new HttpEntity<Msg>(crapport, requestHeaders);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
String response = responseEntity.getBody();
and my object looks something like this (with appropriate getters/setters)
@Root
public class Msg {
@Element
private String comment;
@Element
private String address;
@Element
ByteArrayResource image;
}
Do I need to use a multipart to send strings combined with an byte array or is there some other way to do it?
I haven't tried excplicitly setting additional messageconverters, and it seems (by looking at the source) that string and bytearray message converters are added as default anyway.