Retrofit is a very useful HTTP calling component. The built-in is actually okhttp. Encapsulate the okhttp to make daily business interactions less aware of HTTP calls.
An HTTP call requires just one line of code. The use of specific retrofit is not explained here. Self-Baidu.
This explains only how to enable retrofit to print related parameter logs for HTTP calls. Lecturing
Retrofit each call will be used to okhttpcilent this client, it is the default clients do not have the print log function, view retrofit Source:
Public Final classretrofit{ Public Static Final classBuilder { PublicBuilder Client (okhttpclient client) {returnCallfactory (OKHTTP3. Call.factory) utils.checknotnull (client, "client = = NULL")); } PublicBuilder callfactory (OKHTTP3. Call.factory Factory) {callfactory= (OKHTTP3. call.factory) Utils.checknotnull (Factory, "Factory = = NULL"); return This; }
You can see that this is support for incoming custom okhttpclient, so let's build a okhttpclient that can print the log, and then let each retrofit call print the log.
So how do you build okhttpclient that can print logs? To see the source code of Okhttpclient, we can find the following methods:
Public Builder Addinterceptor (Interceptor Interceptor) { Interceptors.add (Interceptor); return This ; }
This method can add a variety of interceptors to the client and then achieve some of its own needs. We're going to add a log interceptor here, and then every call to the client for some HTTP steps will call this interceptor to do the log output.
The interceptor of the log needs to be introduced into the jar package (this jar package has only one Httplogginginterceptor class ...). Why not just put it in the okhttp bag. ):
<Dependency> <groupId>Com.squareup.okhttp3</groupId> <Artifactid>Logging-interceptor</Artifactid> <version>3.1.2</version></Dependency>
Then we create a httplogginginterceptor of our own and put it into the custom client and set it to retrofit. This way our retrofit can print the log. The specific implementation code is as follows:
Initialize the retrofit and set the okhttpclient that can print the log:
/*** Call Third Party services * *@parambaseurl*@paramservice*@return*/ Public Static<T> T Start (String BaseURL, class<t>service) {Httplogginginterceptor Logginginterceptor=Newhttplogginginterceptor (). SetLevel (HttpLoggingInterceptor.Level.BODY); Okhttpclient client=NewOkhttpclient.builder (). Addinterceptor (Logginginterceptor). build ();return NewRetrofit.builder (). BASEURL (BASEURL). Client (client). Addconverterfactory (Gsonconverterfactory.create ()). Build (). Create (service);}
This setting allows us to print the HTTP call log for retrofit. There is an enumeration of httplogginginterceptor.level in the code. This is where you set the contents of the log to be printed. Briefly introduced below:
HttpLoggingInterceptor.Level.NONE does not print,
HttpLoggingInterceptor.Level.BASIC requests and responses,
HttpLoggingInterceptor.Level.HEADERS Request and Response + header,
HttpLoggingInterceptor.Level.BODY Request and Response + header + body,
People can print according to their own needs.
This allows you to see the log on the console when you start debugging locally. However, if the log logs are to be output to the log file when it is not good to make. Because the interceptor defaults to output to the console.
So how to solve this problem? Let's look at the source code of Httplogginginterceptor:
Public Final classHttplogginginterceptorImplementsinterceptor{ Public Static InterfaceLogger { Public Abstract voidlog (String s); Public Static FinalLogger DEFAULT =NewLogger () { Public voidlog (String message) {platform.get (). log (message); } }; }
Here is an implementation class for the output log interface, we can implement the Httplogginginterceptor.logger interface, and then rewrite its log method to use its own log output tool to output. This will solve the problem. Specifically how to rewrite ...
You don't even have to rewrite the Java code to write ....
Retrofit tool adds HTTP call log output