Weird characters in Retrofit response
<p>My app calls a web service, that returns a file, in bytes. That file can be pdf, or JPG. I am using Retrofit. This is how I am configuring the call:</p>

<pre><code>public static final InterfazGibMobHttp getRetrofitClientUtf(Context c){

    OkHttpClient client = new OkHttpClient();
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    settings = c.getSharedPreferences(c.getString(R.string.sharedPreferencesName), 0);
    client.interceptors().add(new Interceptor() {

        @Override
        public com.squareup.okhttp.Response intercept(Interceptor.Chain chain) throws IOException {
            com.squareup.okhttp.Response respuesta2 = chain.proceed(chain.request());
            String respuesta=respuesta2.body().string();//Weird characters here
            //Some logging
            }

            com.squareup.okhttp.Response newResponse = respuesta2.newBuilder().body(ResponseBody.create(respuesta2.body().contentType(), respuesta.getBytes())).build();

            return newResponse;
        }
    });
    client.interceptors().add(logging);

    String url=settings.getString("urlServlet", null);
    Retrofit builder = new Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .build();

    final InterfazGibMobHttp interfaz = builder.create(MyInterface.class);
    return interfaz;
}
</code></pre>

<p>When the app receive the response, it should get the bytes, and make a file with them, with this code:</p>

<pre><code>Call&lt;ResponseBody&gt; peticion=RetrofitClient.getRetrofitClientUtf(ViewPagerActivity.this).obtenerFichero(settings.getString("imei", ""),settings.getString("idCliente", ""), object);
peticion.enqueue(new Callback&lt;ResponseBody&gt;() {
@Override
public void onResponse(Response&lt;ResponseBody&gt; response, Retrofit retrofit) {

    if(response.isSuccess()){

        ResponseBody body=response.body();
        File dir = new File(Environment.getExternalStorageDirectory() + “/nameOfApp/“+variable+"/"+anocherVariable);


        if (!dir.exists()) {

            dir.mkdirs();
        }

        File file = new File(dir, objetoFichero.get("nombre").getAsString());

        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            fos=new FileOutputStream(file);
            fos.write(body.bytes());
            fos.flush();
            fos.getFD().sync();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    anotherVariable+=1;
}

@Override
public void onFailure(Throwable t) {

    t.printStackTrace();
}
});
</code></pre>

<p>The file is created, but when I try to open the file it is not properly created. It seems to be broken. Checking the response I get, in the String "respuesta" I am receiving some characters that show like "?" with a black background, so could be probably an encoding issue. How could I get my file properly...? Where to play with encoding? The server is sending it with UTF-8, in the interceptor I see the response it is in UTF-8...where is my mistake?</p>

<p>Thank you.</p>