Login System Upgrade 2.0

A previous version of Retrofit uses the RestAdapter and provides the ability to enable logs. Why is this feature removed in Retrofit 2.0 ?

To enable the log, I have to do this.

Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); /** Handles Log */ retrofit.client().interceptors().add(new LoggingInterceptor()); class LoggingInterceptor implements Interceptor { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request request = chain.request(); long t1 = System.nanoTime(); Logger.d(String.format("Sending request %s on %s%n%s", request.url(), chain.connection(), request.headers())); Response response = chain.proceed(request); long t2 = System.nanoTime(); Logger.d(String.format("Received response for %s in %.1fms%n%s", response.request().url(), (t2 - t1) / 1e6d, response.headers())); // Logger.d(""+new String(response.body().bytes())); return response; } 

Is this the only solution for this? The previous position was very convenient ...

+5
android rest retrofit
Oct. 15 '15 at 6:30
source share
4 answers

The intent of the upgrade is to perform safe type de-serialization. He probably abandoned the functions that the http client should perform, for example, this is a log.

Of course, the http client should record the responses received, not the Retrofit. Your question is too broad, Square guys should add more.

+2
Oct 15 '15 at 10:55
source share

To register in modification 2 okhttp has the following registrar:

  private OkHttpClient getOkHttpClient() { HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); if (BuildConfig.LOG_HTTP_CALLS) { logging.setLevel(HttpLoggingInterceptor.Level.BODY); } return new OkHttpClient.Builder() .addInterceptor(logging).build(); } 

and the dependency in build.gradle is

compile 'com.squareup.okhttp3:logginginterceptor:2.1.0'

+2
Aug 11 '16 at 21:50
source share
 OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); if (BuildConfig.DEBUG) { Log.e(getClass().getName(), request.method() + " " + request.url()); Log.e(getClass().getName(), "" + request.header("Cookie")); RequestBody rb = request.body(); Buffer buffer = new Buffer(); if (rb != null) rb.writeTo(buffer); LogUtils.LOGE(getClass().getName(), "Payload- " + buffer.readUtf8()); } return chain.proceed(request); } }) .readTimeout(60, TimeUnit.SECONDS) .connectTimeout(60, TimeUnit.SECONDS) .build(); iIdgardServices = new Retrofit.Builder() .baseUrl("Your Base URL") .client(okHttpClient) .addConverterFactory(GsonConverterFactory.create()) .build() .create(Your Service Interface .class); 

Works for me

0
Aug 11 '16 at 7:48
source share
 private static final String HOST = Urls.HOST; private static final OkHttpClient.Builder mHttpClientBuilder = new OkHttpClient.Builder(); static { //add this line PS: `Level.NONE` is close mHttpClientBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) .addInterceptor(new EncryptInterceptor()) .connectTimeout(30, TimeUnit.SECONDS) .retryOnConnectionFailure(true) .addNetworkInterceptor(new TokenInterceptor()) .authenticator(new DefaultAuthenticator()); } public static <S> S createService(Class<S> serviceClass) { OkHttpClient client = mHttpClientBuilder.build(); Retrofit retrofit = mBuilder.client(client).build(); return retrofit.create(serviceClass); } 

change logcat to DEBUG you can see the log!

0
Sep 02 '16 at 6:46
source share



All Articles