0

I got this error:

java.nio.charset.UnsupportedCharsetException: utf8mb4

And I haven't found any solutions yet.

This says that there's no point to solve this problem, as long as, I can't I change the server side.

How can I handle this charset?

c-an
  • 3,543
  • 5
  • 35
  • 82
  • This charset is just utf8 which some idiot decided to call utf8mb4 instead in whatever context you're dealing with it. You can safely treat the charset as flat-out utf8. – kumesana Feb 13 '19 at 17:03

2 Answers2

0

Try to update your database or run:

ALTER DATABASE yourdatabasename CHARACTER SET utf8 COLLATE utf8_unicode_ci

If you can't change nothing on your backend service, try to change your charset configuration on your request like here:

How to suppress Charset being automatically added to Content-Type in okhttp

Igor Romcy
  • 336
  • 4
  • 7
  • I can't change the server. And I guess the server needs utf8mb4. – c-an Feb 13 '19 at 17:15
  • are you using a cloud service? Is it possible to raise this problem as a dependency? give us more details about ur backend – Igor Romcy Feb 13 '19 at 17:18
  • I don't know about the backend. I am working on frontend. And I just got that format. – c-an Feb 13 '19 at 17:18
  • I don't know how you doing your request but probably you may have to change something like this: https://stackoverflow.com/questions/25560601/how-to-suppress-charset-being-automatically-added-to-content-type-in-okhttp – Igor Romcy Feb 13 '19 at 17:22
  • Thanks. But I don't know how to use them... Can you give me some examples? – c-an Feb 13 '19 at 17:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/188363/discussion-between-chanjung-kim-and-igor-maia-romcy). – c-an Feb 13 '19 at 18:25
0

Here's My solution,

This decodes the response.body() into UTF-8:

String responseResult = null;
try {
    Log.d(TAG, response.body().contentType()+"");
    BufferedSource source = response.body().source();
    source.request(Long.MAX_VALUE); // Buffer the entire body.
    Buffer buffer = source.buffer();
    responseResult = buffer.clone().readString(Charset.forName("UTF-8"));
    Log.d(TAG, "result: "+responseResult);
}catch(Exception e){
    // TODO
}

However, the bigger problem was the URL was incorrect. If it's wrong URL, you can get this error.

For instance,

The URL must be example.com/feed/4 but you sending the request to example.com/feed.

In my case, after decoding the body, I got 404 ERROR. And then, realized the URL was wrong. So, I fixed and It worked fine!

The decode might not convert properly, for example, emojis. Then, try it without converting the body. It will work.

c-an
  • 3,543
  • 5
  • 35
  • 82