日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android调试stetho的那点事

發布時間:2024/3/24 编程问答 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android调试stetho的那点事 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在調試安卓程序的過程中,受不了每次看日志查看網絡的響應和導出db的動作(甚至有時候都無法導出db),這里安利一個facebook出品的神器 stetho,不過這個有些限制,在使用的時候一定的通過usb與調試的手機相連通的,也要使用chrome瀏覽器

1. stetho支持的功能

支持的功能主要是針對網絡和db的,看github項目上的趨勢,應該是在準備一些后續的功能(按照需求集成咯,我覺得網絡和db的最重要了),把官網的介紹摳了下來官網stetho介紹

1. 支持應用的網絡請求返回報文的查看

在chrome瀏覽器中輸入chrome://inspect來進入。(第一次使用這個功能的時候要翻墻,翻墻,翻墻,重要的事情要說三遍,不然你點擊了inpect永遠是空白的,如果沒法翻墻,請查看這篇文章)

2. 支持的db查看功能 和 支持的sql語句直接進行交互功能(增刪改查都是可以的)

2. 集成stetho

  • 導入依賴
    implementation ‘com.facebook.stetho:stetho:1.5.0’

  • 根據網絡請求框架導入不同的依賴包
    implementation ‘com.facebook.stetho:stetho-okhttp3:1.5.0’
    or:
    implementation ‘com.facebook.stetho:stetho-urlconnection:1.5.0’

  • 在application中進行集成

public class MyApplication extends Application {public void onCreate() {super.onCreate();Stetho.initializeWithDefaults(this);} }
  • 如果是有網絡請求的,以okhttp舉例,創建okhttpClient的時候需要加入一個攔截器new OkHttpClient.Builder().addNetworkInterceptor(new StethoInterceptor()).build()

3. 集成stetho的建議(干貨)

  • 建立一個單獨的productFlavor來集成功能,不要在正式的環境中集成這個東西。會使得應用變得更加龐大,也給應用留下漏洞
    比如在build.gradle建立一個productflavor

    productFlavors {{} }

    在應用的的main目錄中建立一個productFlavor innerteset的目錄,然后把,通過清單合并操作中的替換application類的方式重新指定application.

  • 關于網絡的請求中,有的應用的報文是有加解密的。這里需要做一些額外的動作
    修改默認的網絡請求攔截類 StethoInterceptor.class,新建的一個類把原來的類文件中東西拷貝出來進行調整
    解密請求的報文,主要是修改內部類OkHttpInspectorRequest的body(),拿到原始報文的,請求體,完成解密動作后重新包裝生成一個請求體,給原來的代碼使用。下面有一個我在自己應用中使用的實例

    @Nullablepublic byte[] body() throws IOException {//我的測試應用的請求報文都是data:{}的格式,所以這里這么寫,各個應用要按照自己應用的需求改寫FormBody copyedBody = (FormBody) (this.mRequest.body());List<String> nameList = new ArrayList<>();List<String> valusList = new ArrayList<>();for (int i=0; i< copyedBody.size(); i++) {nameList.add(copyedBody.encodedName(i));if ("data".equals(copyedBody.encodedName(i))) {valusList.add(new JsonFormatUtil().formart(這里解密請求的報文));}}FormBody copyedBody2 = new FormBody.Builder().add(nameList.get(0), valusList.get(0)).build();FormBody body = copyedBody2;//下面這塊代碼不動,保持原樣,上面重新生成了requestBody而已if (body == null) {return null;} else {OutputStream out = this.mRequestBodyHelper.createBodySink(this.firstHeaderValue("Content-Encoding"));BufferedSink bufferedSink = Okio.buffer(Okio.sink(out));try {body.writeTo(bufferedSink);} finally {bufferedSink.close();}return this.mRequestBodyHelper.getDisplayBody();}}

    解密返回報文,返回的報文,stetho是保存在文件中的然后進行的發送,需要修改默認的ResponseHandler
    抄襲原來的ReponseHanlder,主要修改的onEOF方式

    //調整原來的類,增加一個readFile的方法public void onEOF() {this.reportDataReceived();try {readFile(this.mRequestId);} catch (IOException e) {Log.e(TAG, "readFile Exception onEOF: " + e);}this.mEventReporter.responseReadFinished(this.mRequestId); }//讀取默認的文件 public ResponseBodyData readFile(String requestId) throws IOException {ResponseBodyFileManager responseBodyFileManager = new ResponseBodyFileManager(CeshiApplication.getApplication());ResponseBodyData responseBodyData = responseBodyFileManager.readFile(requestId);OutputStream outputStream = null;//這個對象是數據的對象,用于json轉換使用SfReponseBodyData sfReponseBodyData = new Gson().fromJson(responseBodyData.data, SfReponseBodyData.class);sfReponseBodyData.data = 這里就可以進行解密的動作,得到解密的字符串;try {outputStream = responseBodyFileManager.openResponseBodyFile(requestId, responseBodyData.base64Encoded);String data = new Gson().toJson(sfReponseBodyData);data = data.replace("\\", "");data = new JsonFormatUtil().formart(data);outputStream.write(data.getBytes());} catch (Exception e) {Log.e(TAG, "readFile Exception: " + e);} finally {if (null != outputStream) {outputStream.close();}}LogUtils.getInstance().showLogD(TAG, "readFile" ,"new record");return null; }

    為了在瀏覽器上好看,報文最后都需要進行格式化,比如我這里是默認的json報文,就進行格式化后傳給瀏覽器

  • //網絡上隨便摳的一段格式代碼 public class JsonFormatUtil {public String formart(String s) {int level = 0;//存放格式化的json字符串StringBuilder jsonForMatStr = new StringBuilder();for (int index = 0; index < s.length(); index++)//將字符串中的字符逐個按行輸出{//獲取s中的每個字符char c = s.charAt(index);//level大于0并且jsonForMatStr中的最后一個字符為\n,jsonForMatStr加入\tif (level > 0 && '\n' == jsonForMatStr.charAt(jsonForMatStr.length() - 1)) {jsonForMatStr.append(getLevelStr(level));}//遇到"{"和"["要增加空格和換行,遇到"}"和"]"要減少空格,以對應,遇到","要換行switch (c) {case '{':case '[':jsonForMatStr.append(c + "\n");level++;break;case ',':jsonForMatStr.append(c + "\n");break;case '}':case ']':jsonForMatStr.append("\n");level--;jsonForMatStr.append(getLevelStr(level));jsonForMatStr.append(c);break;default:jsonForMatStr.append(c);break;}}return jsonForMatStr.toString();}private static String getLevelStr(int level) {StringBuilder levelStr = new StringBuilder();for (int levelI = 0; levelI < level; levelI++) {levelStr.append("\t");}return levelStr.toString();} }

    總結

    以上是生活随笔為你收集整理的android调试stetho的那点事的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。