Android json utf8

Asked

Viewed 234 times

2

Good afternoon! I have a class that imports data json, but some characters are not recognized, I have to apply utf8 to them, but I’m not getting, below the function that matters the data.

public void carregarCategorias() {
    JsonArrayRequest movieReq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                    for (int i = 0; i < response.length(); i++) {
                        try {
                            JSONObject obj = response.getJSONObject(i);
                            categoria = new Categoria();
                            categoria.setIcone(obj.getString("icone"));
                            categoria.setCategoria(obj.getString("categoria"));
                            categoria.setEmpresas(obj.getString("empresas"));
                            categoriaList.add(categoria);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    // Notificando adaptador lista sobre as alterações de dados
                    // Para que ele torna a exibição de lista com dados atualizados
                    adapter.notifyDataSetChanged();

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    AppController.getInstance().addToRequestQueue(movieReq);
    itemClick();
}

Please, somebody help me!

1 answer

0


I already faced this problem once, and solved it by converting the string to bytes in the ISO-8859-1 standard and then creating a new String. I created a method that returns String in UTF-8:

public static RetornaStringUtf8JSon(JSONObject obj, String key) {
    return new String(obj.getString(key).getBytes("ISO-8859-1"), "UTF-8"); 
}

Reference: https://stackoverflow.com/questions/9069799/android-json-charset-utf-8-problems

Browser other questions tagged

You are not signed in. Login or sign up in order to post.