Git Product home page Git Product logo

okhttp-logger-interceptor's Introduction

okhttp-logger-interceptor

在开发中,当访问网络时,如有请求、返回日志,则能及时地了解请求状况,返回结果,提升开发的效率,省去抓包的烦恼.以下描述的问题为Okhttp中遇到的

Square有提供一个 okhttp-logging-interceptor .

效果如图

okhttp-logging-interceptor

一般开发人员最关心的是返回的body部分,传输数据格式通常为json,在Logcat下没有格式化展示,如果数据过长,甚至不能完全展示,这给我们想直接GsonFormat生成bean造成了不便.


所以,在okhttp-logging-interceptor 的基础上做了删改,然后弄了一个简易的 okhttp-logger-interceptor ,如图

okhttp-logger-interceptor

输出日志将json格式化输出,更加清晰.

Download

下载并导入库

或者 Maven

<dependency>
  <groupId>com.lhalcyon</groupId>
  <artifactId>okhttp-logger-interceptor</artifactId>
  <version>1.0.2</version>
  <type>pom</type>
</dependency> 

或者 通过Gradle

compile 'com.lhalcyon:okhttp-logger-interceptor:1.0.2'

Usage

 OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new HttpLogInterceptor()) 
                .build();

无参构造默认使用的是android.util.Log .对于有日志开关需求的,可以传入一个com.halcyon.logger.ILogger的实现类.LogUtilsSample中的自定义日志工具类,开关为BuildConfig.DEBUG

OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new HttpLogInterceptor(new ILogger() {
                    @Override
                    public void d(String tag, String msg) {
                        //自定义的日志类
                        LogUtils.d(msg);
                    }
                }))
                .build();

ProGuard

首先是Okhttp的混淆配置

# okhttp
-dontwarn com.squareup.okhttp.**
-keep class com.squareup.okhttp.** { *;}
-dontwarn okio.**

再就是如果有自定义工具类,打包的时候需要将自定义的日志类的开关置false,然后

#remove Log
-assumenosideeffects class [自定义的日志类如Sample中的(com.example.interceptor.utils.LogUtils)]{
    *;
}

okhttp-logger-interceptor's People

Contributors

lhalcyon 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.