一、编写工具的初衷        

作为一名开发,和后台做接口联调的时候,多多少少会遇到一些问题,甚者,发生一些小摩擦。


        后台:“你看看日志去,我这代码没问题”“给你IP,打个测试包”“打个断点,看看是不是字段不对”
        Android:“好的”“好的,大佬”“我***”
 
        还在用Android Studio看接口日志?一款可视化的Debug调试工具,何不尝试一下?既方便了Android开发,也方便了后台,测试等相关人员,好处良多(PS:终于不再打扰我写BUG了)

二、下面是工具效果图,内容简约

三、使用规则介绍

硬性要求:必须支持okhttp
最新版本:1.1.0

引入依赖
在app下的build.gradle中

dependencies {    //Debug调试界面工具    implementation 'com.potato.apiLog:apiLog:1.1.0'    //编译期间生成class(这个上传的jitpack)    annotationProcess 'com.github.Potato-2020:ApiCompiler:1.0'}

在根目录下的build.gradle中

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

四、代码中的使用

在网络接口类中
接口管理类,可以是接口、抽象类,类。但是要在接口方法上,加上@ApiLog注解

/** * create by Potato * create time  * Description:接口管理 */public interface ApiManager {    //首页信息@nameChinese:接口的中文名字;@nameEnglish:接口的url(注意:不能有host)    //目前的接口host的后缀,没有做兼容,仅支持,如****.com 或 ***.in 或 ***:8080(8082)     @ApiLog(nameChinese = "首页信息", nameEnglish = "/api/example/mainIndex")    @FormUrlEncoded    @POST("/api/example/mainIndex")    Flowable getHome(@FieldMap Map params);}

为每个接口加上ApiLog注解后,build一下项目,会生成一个类ApiLogMap

/** * created by Wangguoli.don't delete it,please!!! * Time: 2020年8月19日 星期三 下午03时53分41秒 CST * 编译期间记录了38个接口 */public class ApiLogMap {  public static final Map mapApi = new HashMap<>();  static {    mapApi.put("/api/example/mainIndex", "首页信息");    ......  }}

添加网络拦截器

new OkHttpClient.Builder().addNetworkInterceptor(new ApiLogInterceptor(mContext));

加入网络拦截器后,访问接口后,会将数据存储到本地数据库,方便查新接口详情

在MainActivity中的代码
在MainActivity中,摇一摇(三下,频率不要太快),或者翻一番(正、反、正,频率不要太快),就会进入到DebugActivity了

class MainActivity : AppCompatActivity(), DebugManager.DebugListener {    private var debugManager : DebugManager? = null    private var receiverPotato: ReceiverPotato? = null    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)        //动态注册广播        val intentFilter = IntentFilter()        intentFilter.addAction(ReceiverPotato.ACTION)        receiverPotato = object : ReceiverPotato() {            override fun changeBaseUrl(baseUrl: String?) {                //修改了接口的host,自己处理逻辑(比如切换网络接口环境)                Log.e("Potato>>>baseUrl>>>", baseUrl)            }            override fun changeImageUrl(imageUrl: String?) {                //修改了图片url的host,自己处理逻辑                Log.e("Potato>>>imageUrl>>>", imageUrl)            }            override fun openWebView(h5: String?) {                //跳转webview                Log.e("Potato>>>h5>>>", h5)            }        }        registerReceiver(receiverPotato, intentFilter)        if (debugManager == null) debugManager = DebugManager()        if (debugManager != null) debugManager?.setListener(this, this)    }    override fun onResume() {        super.onResume()        if (debugManager != null) debugManager?.onResume()    }    override fun onPause() {        super.onPause()        if(debugManager != null) debugManager?.onPause()    }    override fun onDestroy() {        super.onDestroy()        //解注册        unregisterReceiver(receiverPotato)        if(debugManager != null) debugManager?.onDestory()    }    override fun debugApiLog() {        //跳转到Debug调试界面        debugManager?.openDebug(            this,            "https://www.baidu.com",//接口的host            "https://www.ailiuynos.cn",//图片url的host            "1.0.0");//app版本号    }}

如果有问题,那么,请理性“喷”我吧。OVER
项目地址在此

更多相关文章

  1. Android(安卓)环信之踩过的坑(一)---配置
  2. java/android 设计模式学习笔记(8)---桥接模式
  3. android 项目中使用到的网络请求框架以及如何配置好接口URL
  4. android 源码设计模式之--代理模式(Proxy)
  5. android回调机制总结
  6. 闲鱼如何在2个月内实现Android启动速度翻倍的?
  7. android调用webservice实现手机归属查询
  8. 实现android上传多张图片和文字给php后台
  9. Android后台保活机制,应用进程长存的可行性分析

随机推荐

  1. android 中文api (62) ―― ViewSwitcher
  2. Android滚动显示数字动画
  3. android自学笔记《二》——开发环境的搭
  4. android(11)_文件操作读取模式
  5. android webview 使用以及一些常见的异常
  6. android 查看其他进程内存数据
  7. android 模拟器不能上网问题
  8. 在android使用i2c tools访问i2c
  9. android google map的使用
  10. 细数Android Bug