Learn the Interceptor function in retrofit: Implement the print request header content in the log
Retrofit is based on the Okhttp package, so that is to request interception and then print out, you must start from the okhttp.
1. Add a Dependency package:
' com.squareup.okhttp3:logging-interceptor:3.1.2 '
2, I'm here to create a new method for creating and getting okhttp
Privateokhttpclient getokhttpclient () {//Log Display levelHttplogginginterceptor.level level=HttpLoggingInterceptor.Level.BODY; //New Log InterceptorHttplogginginterceptor logginginterceptor=NewHttplogginginterceptor (NewHttplogginginterceptor.logger () {@Override Public voidlog (String message) {LOG.D ("ZCB","Okhttp====message:"+message); } }); Logginginterceptor.setlevel (level); //Custom OkhttpOkhttpclient.builder Httpclientbuilder =Newokhttpclient. Builder (); //okhttp to add interceptors LogginginterceptorHttpclientbuilder.addinterceptor (Logginginterceptor); returnHttpclientbuilder.build (); }
3, to use the acquired okhttp to retrofit
Retrofit retrofit=New retrofit.builder () . BASEURL (Appconfig.base_url) . Addconverterfactory ( Gsonconverterfactory.create ()) . Client (Getokhttpclient ())// Use the okhttp. Build that you created ();
: I have initiated three requests.
Retrofit2.0+okhttp Print Request URL (requested address parameter)