Results 1 to 3 of 3

Thread: Upload file to JSON web service and gets a boolean as return

Hybrid View

  1. #1
    Join Date
    May 2012
    Posts
    9

    Default Upload file to JSON web service and gets a boolean as return

    I'm very new on Spring for Android development.

    I want to upload and image to a JSON web service and I have write down this code:

    Code:
    public static Boolean UploadFile(String url, String filePath)
    {
    	Log.v("OrderSpringController", "UploadFile: " + filePath);
    	Boolean result = false;
    
    	try
    	{
    		HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    		requestFactory.setReadTimeout(60000);
    
    		RestTemplate restTemplate = new RestTemplate(requestFactory);
    
    		MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
    		parts.add("file", new ClassPathResource(filePath));
    		restTemplate.postForLocation(url, parts);
    	}
    	catch (Exception e)
    	{
    		e.printStackTrace();
    	}
    	
    	return result;
    }
    I also need to send with image a class indicating image data (this data is image table, image type, etc.)

    But I need to upload it as a JSON POST message. And, when web service gets image it will return a boolean indicating its result.

    I have another method which post a JSON message and returns a boolean:

    Code:
    public static Boolean SendCompletedEReports(String url,
    		EReport[] eReports)
    {
    	Log.v("OrderSpringController", "LoadSelectOrders");
    	try
    	{
    		HttpHeaders requestHeaders = new HttpHeaders();
    		requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
    		HttpEntity<EReport[]> requestEntity = new HttpEntity<EReport[]>(eReports, requestHeaders);
    
    		GsonHttpMessageConverter messageConverter = new GsonHttpMessageConverter();
    		List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
    		messageConverters.add(messageConverter);
    
    		HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    		requestFactory.setReadTimeout(60000);
    
            RestTemplate restTemplate = new RestTemplate(requestFactory);
            restTemplate.setMessageConverters(messageConverters);
    
            ResponseEntity<Boolean> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, Boolean.class);
    
            return responseEntity.getBody();
    	}
    	catch (Exception e)
    	{
    		e.printStackTrace();
    	}
    
    	return null;
    }

    But I don't know how to mix this two methods.

    Any advice?

  2. #2
    Join Date
    Nov 2010
    Posts
    175

    Default

    Do you control the server endpoint? I recommend against trying to post image data in json, if you can help it.
    Roy Clarkson
    Spring Mobile Projects Lead

  3. #3
    Join Date
    Oct 2012
    Posts
    3

    Default

    Did you solve this? I've a similar need: to send in the same http request either an image and an xml object. I've already written the two method separately, but can't get them in one. Is it possible to use a multipart_form_data with a section containing the xml object? Or is it more correct to incapsulate the image inside the xml message as byte array or similar?
    Yes, I control the server endpoint.
    Any advice?
    Thank you.

Posting Permissions

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