Announcement Announcement Module
Collapse
No announcement yet.
Upload file to JSON web service and gets a boolean as return Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Do you control the server endpoint? I recommend against trying to post image data in json, if you can help it.

    Comment


    • #3
      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.

      Comment

      Working...
      X