Code Monkey home page Code Monkey logo

okhttploginterceptor's Introduction

OkHttpLogInterceptor 简体中文

A light weight and practical OkHttp log interceptor


Download

Step 1.

Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Step 2.

Add the dependency in your module's build.gradle:

dependencies {
    implementation 'com.github.zeropercenthappy:OkHttpLogInterceptor:1.6.7'
}

Usage

Kotlin

val logInterceptor = OkHttpLogInterceptor("yourOwnTag")
// add this interceptor
val okHttpClient = OkHttpClient.Builder()
	.addNetworkInterceptor(logInterceptor)
	// or
    // .addInterceptor(logInterceptor)
    .build()

Java

OkHttpLogInterceptor logInterceptor = new OkHttpLogInterceptor("yourOwnTag");
// add this interceptor
OkHttpClient okHttpClient = new OkHttpClient.Builder()
    	.addNetworkInterceptor(logInterceptor)
    	// or
        // .addInterceptor(logInterceptor)
        .build();

Effect

It will print as below:

--> Request for https://api.xxx.com:80
POST /login http/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 56
Host: api.xxx.com
Connection: Keep-Alive
Accept-Encoding: gzip
Cookie: JSESSIONID=9CB615425A54C2BB9E66D43509360D1A
User-Agent: okhttp/4.9.0
 
name=guest&password=123456&extraParamKey=extraParamValue
--> Request end
<-- Response from https://api.xxx.com
http/1.1 200 
Server: Tengine/2.2.2
Date: Wed, 16 Dec 2020 06:59:01 GMT
Content-Type: application/json;charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Set-Cookie: JSESSIONID=CC136BA58768DDD1122D53194438E6F2; Path=/; HttpOnly
 
{"code":200,"succeed":true}
<-- Response end

Other

​ If using addInterceptor()method to add this log interceptor to Application Interceptor, the log print will not inclue standard headers of request, because these headers will be added during OkHttp core flow which is beyond Application Interceptor.

​ Alternative, you can use addNetworkInterceptor() method to add this log interceptor to Network Interceptor. In this case you can see every thing is included in the log print. But if cache is enable and the cache of a request is exist, log interceptor will not print anything. Becasuse cache flow and network flow are exclusive.

okhttploginterceptor's People

Contributors

zeropercenthappy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.