In my app I use Retrofit+Gson to communicate with the server. However, there is encoding problem with the server response.
In OkHttp logging interceptor server response look like this, which is OK:
... "name":"S\u0026#39;mores" ...
This is part of server response JSON. When the response is deserialized by Gson, the deserialized string looks like this:
S'mores
I'd like to know what's happening during deserialization and how to make Strings be encoded properly.
Below is my Retrofit initialization code:
val client = OkHttpClient.Builder()
.addInterceptor(
HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY)
)
mRetrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client.build())
.addCallAdapterFactory(RxJava2CallAdapterFactory.createAsync())
.addConverterFactory(GsonConverterFactory.create())
.build()
mAPI = mRetrofit.create(PetfinderJSONAPI::class.java)
What I tried:
- Set
gson.disableHtmlEscaping()
and pass it toGsonConverterFactory.create()
- Add OkHttp Interceptor which sets
Accept-Charset: utf-8
- Manually decode String before setting it to text field by
URLDecoder.decode()
andHtml.fromHtml()
Neither of these helped. My strings are still not properly encoded.
EDIT: According to this html decoder site, '
is decoded as '
. Thus, I expect my decoded string to be S'mores