1

Android client, using Springs resttemplace and the Apache common HTTP client to make requests.

I'm working against a server, that sometimes returns a 401 error, with a http header string, "ERROR" that contains a user-info string. The string is language dependent, so it might contain, for example, Scandinavian characters.

The string looks fine in my IOS app, aswell as when i examine it in the Firefox RESTclient plugin.

However, in my Android app, i cannot for the life of me get the chars to come out right. I'd very much appreciate it if someone could think of a way i can make the data come out right.

The server sends content-type UTF-8, and its a regular .setHeader() on the httpservletresponse that sets the parameter i try to retrieve.

Here's the creation of my resttemplate in my Android client (ive tried most methods as you can see):

    HttpClient httpClient = new HttpClient();
    Credentials defaultcreds = new UsernamePasswordCredentials(msisdn, password);
    httpClient.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), defaultcreds);
    httpClient.getParams().setSoTimeout(prefs.getServerTimeout());
    httpClient.getParams().setConnectionManagerTimeout(3000);
    httpClient.getParams().setContentCharset("utf-8");
    httpClient.getParams().setCredentialCharset(prefs.getCredentialsEncoding());
    httpClient.getParams().setHttpElementCharset("utf-8");
    httpClient.getParams().setUriCharset("utf-8");
    CommonsClientHttpRequestFactory requestFactory = new CommonsClientHttpRequestFactory(httpClient);
    requestFactory.setReadTimeout(prefs.getServerTimeout());
    RestTemplate restTemplate = new RestTemplate(requestFactory);
    // Add message converters
    List<HttpMessageConverter<?>> mc = restTemplate.getMessageConverters();
    MappingJacksonHttpMessageConverter json = new MappingJacksonHttpMessageConverter();

    List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
    supportedMediaTypes.add(MediaType.APPLICATION_JSON);
    json.setSupportedMediaTypes(supportedMediaTypes);
    mc.add(json);
    restTemplate.setMessageConverters(mc);

    // Set our specific error handler
    restTemplate.setErrorHandler(new MyErrorHandler());

this is my http response, copied from restclient in Firefox if i run the same request there:

Status Code: 401 Unauthorized
Content-Length: 954
Content-Type: text/html;charset=utf-8
Date: Sat, 19 Jan 2013 23:53:10 GMT
ERROR: För att <contents cut out but as you see Scandinavian char looks fine here >
Server: Apache-Coyote/1.1
WWW-Authenticate: Basic realm="rest"
Mathias
  • 3,879
  • 5
  • 36
  • 48

2 Answers2

0

HTTP headers use ASCII (or, in the older specs, Latin-1). Putting other encodings into headers might or might not work.

Mark Nottingham
  • 5,546
  • 1
  • 25
  • 21
  • Yeah i saw that in some spec, US-ascii. I just didn't get why it worked in IOS and Firefox... Got any idea about what i can do to make my Android app read the headers as UTF-8? – Mathias Jan 20 '13 at 11:36
0

Your Response Content-Type appears to be text/html. Did you try setting Content-Type and charset in the request header like below? As per documentation here

Documents transmitted with HTTP that are of type text, such as text/html, text/plain, etc., can send a charset parameter in the HTTP header to specify the character encoding of the document.

LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
headers.set("Content-Type", "text/html;charset=utf-8");
HttpEntity request = new HttpEntity(null, headers);
ResponseEntity<Person> response = template.exchange(url,
                HttpMethod.GET, request, Person.class);
nilesh
  • 14,131
  • 7
  • 65
  • 79