POST request error with JSON with accents in Android

3

I am sending a request to an API in ASP.NET from Android, I am using Spring Framework to make requests, the problem is that when sending a JSON that contains accented characters the server returns an exception with the following message:

{"exception":"Object reference not set to an instance of an object."}

The problem only occurs with the accents, and from the Android app, I sent an identical request (with accents) using Postman, and the server accepts it without problems.

This is the code I'm using in Java:

RestTemplate rest = new RestTemplate();
JSONObject request = new JSONObject()
                    .put("Title", "título")
                    .put("Content", "contenido del mensaje");

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(request.toString(), headers);

ResponseEntity<String> response = rest.exchange("http://elsitio.com/message/post?authToken={authToken}", HttpMethod.POST, entity, String.class, Session.getToken());

I have already tried changing the server language and it does not work.

    
asked by migueladanrm 02.02.2017 в 07:16
source

1 answer

2

Try adding conversion to UTF-8 (or the encoding you need):

RestTemplate rest = new RestTemplate();
rest .getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
    
answered by 02.02.2017 / 08:34
source