Problem with accented text JSON - Android + Retrofit

0

I'm consuming data from a webservice that returns a list with JSON objects, like this:

[{"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 through the browser they are all accented correctly in the response of the request. But when I make the request on Android the response in JSON brings the accented characters replaced with questions, so trying to convert to UTF-8 or ISO-8859-1 in java did not work either. It looks like this:

[{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 am 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 an external solution or the own lib retrofit that I can use to solve it?

    
asked by anonymous 27.09.2017 / 19:46

1 answer

1

Solved using the Interceptor class from the okHttp lib. The class has an intercept () method with which it is possible to manipulate the request response, changing the MediaType to the correct configuration. Here's the 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();
    
27.09.2017 / 23:15