小言_互联网的博客

还在用Android Studio看接口日志?一款可视化的Debug调试工具,何不尝试一下

440人阅读  评论(0)

一、编写工具的初衷        

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


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

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

三、使用规则介绍

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

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


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

在根目录下的build.gradle中


  
  1. allprojects {
  2.     repositories {
  3.         maven { url "https://jitpack.io" }
  4.     }
  5. }

四、代码中的使用

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


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

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


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

添加网络拦截器

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

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

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


  
  1. class MainActivity : AppCompatActivity(), DebugManager.DebugListener {
  2.     private var debugManager : DebugManager? = null
  3.     private var receiverPotato: ReceiverPotato? = null
  4.     override fun onCreate(savedInstanceState: Bundle?) {
  5.         super.onCreate(savedInstanceState)
  6.         setContentView(R.layout.activity_main)
  7.         //动态注册广播
  8.         val intentFilter = IntentFilter()
  9.         intentFilter.addAction(ReceiverPotato.ACTION)
  10.         receiverPotato = object : ReceiverPotato() {
  11.             override fun changeBaseUrl(baseUrl: String?) {
  12.                 //修改了接口的host,自己处理逻辑(比如切换网络接口环境)
  13.                 Log.e( "Potato>>>baseUrl>>>", baseUrl)
  14.             }
  15.             override fun changeImageUrl(imageUrl: String?) {
  16.                 //修改了图片url的host,自己处理逻辑
  17.                 Log.e( "Potato>>>imageUrl>>>", imageUrl)
  18.             }
  19.             override fun openWebView(h5: String?) {
  20.                 //跳转webview
  21.                 Log.e( "Potato>>>h5>>>", h5)
  22.             }
  23.         }
  24.         registerReceiver(receiverPotato, intentFilter)
  25.         if (debugManager == null) debugManager = DebugManager()
  26.         if (debugManager != null) debugManager?.setListener( this, this)
  27.     }
  28.     override fun onResume() {
  29.         super.onResume()
  30.         if (debugManager != null) debugManager?.onResume()
  31.     }
  32.     override fun onPause() {
  33.         super.onPause()
  34.         if(debugManager != null) debugManager?.onPause()
  35.     }
  36.     override fun onDestroy() {
  37.         super.onDestroy()
  38.         //解注册
  39.         unregisterReceiver(receiverPotato)
  40.         if(debugManager != null) debugManager?.onDestory()
  41.     }
  42.     override fun debugApiLog() {
  43.         //跳转到Debug调试界面
  44.         debugManager?.openDebug(
  45.             this,
  46.             "https://www.baidu.com", //接口的host
  47.             "https://www.ailiuynos.cn", //图片url的host
  48.             "1.0.0"); //app版本号
  49.     }
  50. }

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


转载:https://blog.csdn.net/qq_29266921/article/details/108128941
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场