Problem with accented JSON text - Android + Retrofit

Asked

Viewed 488 times

0

I am consuming data from a webservice that returns a list with JSON objects, this way:

[{"idPDV":3062,"nomeFantasia":"SEBASTIÃO  JOSÉ DOS SANTOS","endereco":"RUA MANCHE CATAN DAVID, 130","bairro":"VIDA NOVA","CEP":"79017164","latitude":"-20.382218","longitude":"-54.569492"}]

Some of these guys have accentuation in the text and accessing the URL by the browser they come all accented correctly in the response to the request. But when I request on Android the answer in JSON brings the accented characters replaced by queries, so that trying to convert to UTF-8 or ISO-8859-1 in java also did not work. Stay that way:

[{idPDV=3062, nomeFantasia=SEBASTI�O  JOS� DOS SANTOS, endereco=RUA MANCHE CATAN DAVID, 130, bairro=VIDA NOVA, CEP='79017164, latitude='-20.382218, longitude='-54.569492'}]

This is the code where I’m retrieving the data:

retrofit = new Retrofit.Builder()
        .baseUrl("http://minhaurl")
        .addConverterFactory(GsonConverterFactory.create())
        .build();

PdvsService service = retrofit.create(PdvsService.class);
Call<List<PontoDeVenda>> call = service.allPdvs();

call.enqueue(new Callback<List<PontoDeVenda>>() {
    @Override
    public void onResponse(Call<List<PontoDeVenda>> call, Response<List<PontoDeVenda>> response) {

        for (PontoDeVenda pdv : response.body()) {
            Log.d("PDV", pdv.toString());
        }

        PdvArrayAdapter arrayAdapter = new PdvArrayAdapter(getActivity().getApplicationContext(), R.layout.item_lista_pdv, response.body());
            arrayAdapter.clear();
            lstPdvs.setAdapter(arrayAdapter);
            setOffLoading();

    }

    @Override
    public void onFailure(Call<List<PontoDeVenda>> call, Throwable t) {
        Log.e("Erro", t.getMessage());
    }
});

Is there any external or own retrofit lib solution I can use to solve?

1 answer

1


Solved using the Interceptor class of the okHttp lib. The class has an Intercept() method with which it is possible to manipulate the request response by changing Mediatype to the correct configuration. Follows final solution:

OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder();
okHttpBuilder.addInterceptor(new Interceptor() {

@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    okhttp3.Response response = chain.proceed(request);

    MediaType mediaType = MediaType.parse("application/json; 
    charset=iso-8859-1");
    ResponseBody modifiedBody = ResponseBody.create(mediaType, 
    response.body().bytes());
    okhttp3.Response modifiedResponse = response.newBuilder()
            .body(modifiedBody)
            .build();

    return modifiedResponse;
    }
});

retrofit = new Retrofit.Builder()
    .baseUrl("http://minhaurl")
    .addConverterFactory(GsonConverterFactory.create())
    .client(okHttpBuilder.build())
    .build();

Browser other questions tagged

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