PDA

View Full Version : Needs feature to specify charset in StringHttpMessageConverter



kingori
Apr 7th, 2012, 03:31 AM
Hi, there. I'm using Spring Android 1.0.0RC1 and it works great. thanks.

And I suggests some improvement in StringHttpMessageConverter.

In default constructor of StringHttpMessageConverter, it searches availableCharsets using Charset.availableCharsets().


public StringHttpMessageConverter() {
super(new MediaType("text", "plain", DEFAULT_CHARSET), MediaType.ALL);
this.availableCharsets = new ArrayList<Charset>(Charset.availableCharsets().values());
}

And in my profiling result, this method spent 50% of time in creating RestTemplate instance. So I planned to make my own StringHttpMessageConverter (I just use UTF-8 in all requests), but RestTemplate create StringHttpMessageConverter in its constructor.


public RestTemplate() {
this.messageConverters.add(new ByteArrayHttpMessageConverter());
this.messageConverters.add(new StringHttpMessageConverter());
this.messageConverters.add(new ResourceHttpMessageConverter());

More over, Charset.availableCharsets() uses native icu4j method and it sometimes fail. So if I could specify charset in somewhere, or just not using Charset.availableCharsets(), I can improve performance and some problems that icu4j makes. Thanks.

Roy Clarkson
Apr 17th, 2012, 10:14 AM
@kingori thanks for the feedback! It is much appreciated. Since Rest Template in Spring for Android was ported from Spring Framework, we are continuing to identify areas for performance improvement. I've created a JIRA [1] to track this for inclusion in the next release.

[1] https://jira.springsource.org/browse/ANDROID-82

Roy Clarkson
Apr 30th, 2012, 08:32 PM
I made some changes to support creating a custom StringHttpMessageConverter. I added two constructors, allowing you to set a default Charset, and a list of available Charsets. Setting the list prevents Charset.availableCharsets() from being called. I'm also evaluating how to modify RestTemplate to prevent the default StringHttpMessageConverter from being created.

https://jira.springsource.org/browse/ANDROID-88

Roy Clarkson
Jun 12th, 2012, 01:30 PM
@kingori, can you provide more details for which Android versions and devices you were experiencing the poor performance from Charset.availableCharsets()? Even though I've already made a change for this, I would like to document the details in the JIRA. Thanks!

kingori
Jun 13th, 2012, 09:48 PM
@Roy Clarkson

I've made a very simple android project to measure Charset.availableCharsets() * 10 times and to reproduce crash situation.

https://gist.github.com/2927754

On android 2.3.4, samsung galaxy s-II , execution took 175ms. I use Korean location. And then I press button, I've got the crash message at the bottom of this meesage.

I assumed this crash is caused by Charset.availableCharsets() as the line "/system/lib/libicuuc.so" is related to native ICU library and it is used by Charset.availableCharsets(). In this sample, crash occurred after 11 attempt to call the method. But in my real application, this error occurred more easy (less threads running) situation.

So I solved this problem by making my custom StringHttpMessageConverter, RestTemplate. But the new SpringAndroid solved it all. Thanks very much!




....
06-14 11:34:02.236: D/charset(13485): And_charset_comparisonActivity.run: 11
...
06-14 11:34:02.436: I/DEBUG(12725): stopped -- continuing
06-14 11:34:02.436: I/DEBUG(12725): waitpid: n=13585 status=00000b7f
06-14 11:34:02.436: I/DEBUG(12725): stopped -- fatal signal
06-14 11:34:02.436: I/DEBUG(12725): pid: 13485, tid: 135[QUOTE]85 >>> kr.pe.kingori.exp.charset <<<
06-14 11:34:02.436: I/DEBUG(12725): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000014
...
06-14 11:34:02.436: I/DEBUG(12725): scr 20000010
06-14 11:34:02.521: I/DEBUG(12725): #00 pc 0003443e /system/lib/libicuuc.so

Roy Clarkson
Jun 15th, 2012, 01:59 PM
Great. Thanks for the information!