2

I'm building an android app using Retrofit2 to communicate with a REST API. Everything is working well except when the app receives a text/plain response with a custom message. The header tells me that content-type is in UTF-8 but when I get the Retrofit.message() it prints the message with � characters.

When I make a POST request using Postman the message is right.

Status: 400 Código de documento não localizado.

Retrofit instance is created like this:

    val instance: Retrofit by lazy {
        Retrofit.Builder()
            .baseUrl(createBaseUrl())
            .client(createClient())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    private fun createClient() = OkHttpClient.Builder()
        .connectTimeout(BuildConfig.CONNECTION_TIMEOUT, TimeUnit.SECONDS)
        .readTimeout(BuildConfig.READ_TIMEOUT, TimeUnit.SECONDS)
        .writeTimeout(BuildConfig.READ_TIMEOUT, TimeUnit.SECONDS)
        .build()

    private fun createBaseUrl() = HttpUrl.get(BuildConfig.API_URL)

And I handle the responde this way:

        val response = call().execute()
        if (response.isSuccessful)
            success(response.body())
        else
            Result.Failure.Response(response.code(), response.message())

For example, I got a response like this:

Headers:

Cache-Control: no-cache
Pragma: no-cache
Content-Type: text/plain; charset=utf-8
Expires: -1
Server: Microsoft-IIS/10.0
X-BuildInfo: 1.0.0.3473-20190131-140734
X-Frame-Options: DENY
Date: Thu, 13 Jun 2019 12:40:16 GMT
Content-Length: 0
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN

Raw response:

Code: 400 Message: C�digo de documento n�o localizado.

The message should be printed this way: Código de documento não localizado.

I don't know if the problem is retrofit or gson-converter, but I didn't find out what is wrong or how to solve this problem.

Could someone help me to solve this problem, please?

3iL
  • 2,146
  • 2
  • 23
  • 47

0 Answers0