Announcement Announcement Module
Collapse
No announcement yet.
Get JSON Response when sending a MULTI_PART_DATA Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Get JSON Response when sending a MULTI_PART_DATA

    I'm following this example, https://github.com/SpringSource/spri...aActivity.java to send an Image to a WCF RestFul web service.
    Code:
    [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "uploadImage/")]
    Boolean UploadImage(Stream fileContents);
    As you can see, I want to return a Boolean to indicate if image has been saved successfully or not. But, when I've tried to update code to get JSON response, I get an exception:

    This is my code to send image to server:

    Code:
    public static Boolean sendImage(String url, String filePath, long eReportId)
    {
    	try
    	{
    		MultiValueMap<String, Object> formData;
    
    		Resource resource = new FileSystemResource(filePath);
    
    		// populate the data to post
    		formData = new LinkedMultiValueMap<String, Object>();
    		formData.add(OrderSpringController.FILE, resource);
    
    		HttpHeaders requestHeaders = new HttpHeaders();
    		requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
    
    		// Sending multipart/form-data
    		requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
    
    		// Populate the MultiValueMap being serialized and headers in an HttpEntity object to use for the request
    		HttpEntity<MultiValueMap<String, Object>> requestEntity = 
    				new HttpEntity<MultiValueMap<String, Object>>(formData, requestHeaders);
    
    		GsonHttpMessageConverter messageConverter = new GsonHttpMessageConverter();
    		List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
    		messageConverters.add(messageConverter);
    
    		// Create a new RestTemplate instance
    		RestTemplate restTemplate = new RestTemplate(true);
            restTemplate.setMessageConverters(messageConverters);
    
    		// Make the network request, posting the message, expecting a String in response from the server
    		ResponseEntity<Boolean> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity,
    				Boolean.class);
    
    		// Return the response body to display to the user
    		return response.getBody();
    	}
    	catch (Exception ex)
    	{
    		ex.printStackTrace();
    	}
    	return null;
    }
    And this is the exception:
    Code:
    org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.util.LinkedMultiValueMap] and content type [multipart/form-data]
    	at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:632)
    	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:473)
    	at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:438)
    	at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:414)
    	at es.viacognita.ereports.webservice.OrderSpringController.sendImage(OrderSpringController.java:215)
    	at es.viacognita.ereports.EReportsActivity$SendImageAsynTask.doInBackground(EReportsActivity.java:113)
    	at es.viacognita.ereports.EReportsActivity$SendImageAsynTask.doInBackground(EReportsActivity.java:1)
    	at android.os.AsyncTask$2.call(AsyncTask.java:252)
    	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    	at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
    	at java.lang.Thread.run(Thread.java:1020)
    I'm very very new on SpringFramework for Android development.

    How can I do to get server's JSON response?
    Last edited by VansFannel; Nov 27th, 2012, 06:00 AM.
Working...
X