Results 1 to 5 of 5

Thread: Using the android rest template to make a MultiPart request

  1. #1
    Join Date
    May 2009
    Posts
    19

    Default Using the android rest template to make a MultiPart request

    Hi all

    been at this for about a day, trying to get the resttemplate to post my multipart request.

    heres the code that im using as a template.

    Code:
    RestTemplate template = new RestTemplate();
    String uri = "http://localhost:8080/multipart-http/inboundAdapter.htm";
    Resource s2logo = 
       new ClassPathResource("org/springframework/samples/multipart/spring09_logo.png");
    MultiValueMap map = new LinkedMultiValueMap();
    map.add("company", "SpringSource");
    map.add("company-logo", s2logo);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(new MediaType("multipart", "form-data"));
    HttpEntity request = new HttpEntity(map, headers);
    ResponseEntity<?> httpResponse = template.exchange(uri, HttpMethod.POST, request, null);
    My only differences are that i set headers for authentication and that I cant use the FileSystemResource to specifiy my file....

    everytime i execute the code i get

    Code:
    04-12 02:06:37.762: ERROR/BaseListActivity(6543): Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.util.LinkedMultiValueMap] and content type [multipart/form-data]
    im using M2 and the file that i want to upload is not on my Classpath.

    .... id really appreciate the help

    Many thanks as allways

  2. #2
    Join Date
    Nov 2010
    Posts
    175

    Default

    Your code requires the use of the FormHttpMessageConverter to post the form data. The FormHttpMessageConverter was not being automatically added to the MessageConverters collection when you created a new RestTemplate instance in M2. The M3 release now adds this by default. If you are on API Level 8 or higher, then it adds the XmlAwareFormHttpMessageConverter which has additional XML support in form data. I've also added an example in the showcase application that basically implements your code. The feedback is always appreciated!

  3. #3
    Join Date
    May 2012
    Posts
    1

    Default

    Is there no support to parse a POJO and use it?

    I used this example now:
    Code:
     MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
     parts.add("field 1", "value 1");
     parts.add("file", new ClassPathResource("myFile.jpg"));
     template.postForLocation("http://example.com/myFileUpload", parts);
    But how to fetch the Respone which is JSON in my case.

    Thanks for your help.
    Daniel
    Last edited by drindt; May 2nd, 2012 at 02:48 AM.

  4. #4
    Join Date
    Nov 2010
    Posts
    175

    Default

    Yes, you should be able to use one of the JSON message converters to convert the response to a POJO. Take a look at the postForObject(...) method [1] or even the exchange(...) method for doing this.

    [1] [url]http://static.springsource.org/spring-android/docs/1.0.x/api/org/springframework/web/client/RestTemplate.html
    Last edited by Roy Clarkson; May 10th, 2012 at 11:36 AM. Reason: url formatting issues
    Roy Clarkson
    Spring Mobile Projects Lead

  5. #5
    Join Date
    May 2012
    Posts
    9

    Default

    I have the same problem.

    How do you solved it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •