android網絡調試一直是一個比較麻煩的部分,由於在不一樣序列的請求中,返回的數據會有不一樣的變化,若是能像web開發同樣使用調試功能查看頁面的訪問數據該是多麼美好的事情!java
package com.peiandsky.chromedebug; import android.app.Application; import com.facebook.stetho.Stetho; public class App extends Application { @Override public void onCreate() { super.onCreate(); Stetho.initialize(Stetho .newInitializerBuilder(this) .enableDumpapp(Stetho.defaultDumperPluginsProvider(this)) .enableWebKitInspector( Stetho.defaultInspectorModulesProvider(this)).build()); } }
package com.peiandsky.chromedebug; import java.io.IOException; import com.facebook.stetho.okhttp.StethoInterceptor; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.Response; public class Net { private static final boolean debug = true; private static OkHttpClient okHttpClient = new OkHttpClient(); static { if (debug) { okHttpClient.networkInterceptors().add(new StethoInterceptor()); } } public static final void askBaidu() { Request request = new Request.Builder().url("http://www.baidu.com") .build(); try { Response response = okHttpClient.newCall(request).execute(); String reslut = response.body().string(); } catch (IOException e) { e.printStackTrace(); } } }