Announcement Announcement Module
Collapse
No announcement yet.
Using the android rest template to make a MultiPart request Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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!

    Comment


    • #3
      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, 03:48 AM.

      Comment


      • #4
        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, 12:36 PM. Reason: url formatting issues

        Comment


        • #5
          I have the same problem.

          How do you solved it?

          Comment

          Working...
          X