Retrofit 2 RequestBody writeTo() method called twice Retrofit 2 RequestBody writeTo() method called twice android android

Retrofit 2 RequestBody writeTo() method called twice


The solution below might help you out , although it might be too late. :p

Remove HttpLoggingInterceptor Object in your Api Client which will not execute writeTo() function twice.Basically , HttpLoggingInterceptor loads the data buffer first ( for internal logging purpose ) by calling writeTo() and then again calls writeTo() for uploading the data to server.

  HttpLoggingInterceptor logging = new HttpLoggingInterceptor();  logging.setLevel(HttpLoggingInterceptor.Level.BODY);  httpClient.addInterceptor(logging);


Decreasing log level from BODY to HEADERS, BASIC or NONE solved this problem for me


I figured out yet another case for twice called writeTo() method.I use OkHttpClient without Retrofit and HttpLoggingInterceptor, and I have the twice called problem.

Solution: the problem appears after upgrade Android Studio to 3.1.1 and enable Advanced Profiling in run project configuration. So disable Advanced Profiling.