Announcement Announcement Module
Collapse
No announcement yet.
POST JSON data to a Web Service Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • POST JSON data to a Web Service

    I'm developing an Android application that uses SpringFramework Android Rest Client.

    This is my method:
    Code:
    	public static User sendUserPersonalData(User userProfileData)
    	{
    		User user = null;
    		try
    		{
    			HttpHeaders requestHeaders = new HttpHeaders();
    			requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
    			HttpEntity<User> requestEntity = new HttpEntity<User>(userProfileData, requestHeaders);
    
    			GsonHttpMessageConverter messageConverter = new GsonHttpMessageConverter();
    			List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
    			messageConverters.add(messageConverter);
    
    			HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    			requestFactory.setReadTimeout(readTimeout);
    
    	        RestTemplate restTemplate = new RestTemplate(requestFactory);
    	        restTemplate.setMessageConverters(messageConverters);
    	        //restTemplate.setErrorHandler(new CustomResponseErrorHandler());
    
    	        String url = URL_BASE_WEB + USER_PERSONAL_DATA_CALL;
    	        ResponseEntity<User> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, User.class);
    	        user = responseEntity.getBody();
    	        // We have sent a valid user.
    	        user.setValid(true);
    
    	        return user;
    		}
    		catch (RestClientException e)
    		{
    			e.printStackTrace();
    		}
    
    		return user;
    	}
    Now, I'm sending my data as a form but I want to send it as a JSON. How can I do it?
Working...
X