user7702411
user7702411

Reputation:

Converting json String to UTF-8 in Android

I want show message from server into my application, and i should show this message from json.
But message text such as this : &#274 4;&#276 5;&#272 5;&#273 9;

I use this code for show message text from json :

public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {

    ServerResponse resp = response.body();

    if (resp.getResult().equals("success")) {
        Snackbar.make(getView(), reps.getMessage(), Snackbar.LENGTH_LONG).show();

but show &#274 4;&#276 5;&#272 5;&#273 9; in Snackbar!!

how can i fix it?

Upvotes: 0

Views: 499

Answers (2)

MarkySmarky
MarkySmarky

Reputation: 1629

You can try and create a new String object out of the response message.

String value = new String(reps.getMessage().getBytes("UTF-8"));

assuming that 'reps.getMessage()' returns String

Upvotes: 0

xcesco
xcesco

Reputation: 4838

You have to unscape character before display string. You can read this.

Just to summarize. I suggest you to include in your project the jar commons-lang of Apache foundation in compile dependencies:

compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'

and then modify your code in this way:

import org.apache.commons.lang3.StringEscapeUtils;

...

public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
  ServerResponse resp = response.body();
  if (resp.getResult().equals("success")) {
    Snackbar.make(getView(), StringEscapeUtils.unescapeHtml4(reps.getMessage()), Snackbar.LENGTH_LONG).show();

Upvotes: 1

Related Questions