Android network debugging has always been a troublesome part, because in different sequences of requests, the returned data will vary, if you can use the debugging function like Web development to view the page access data what a wonderful thing!
Fortunately, now Android development can also listen to network access in real time, to see your sending data information, but also to see the return data information. Click on each request to see the detailed page, you can view the details of the request, if you want to achieve the above effect, you need to transform your network request module, using Chrome browser and Android program between the middleware to connect, this is the topic of this article: okhttp+stetho+ Chrome for network debugging. Okhttp is a very good network access framework for square, which is very easy to use and can get its source code via GitHub: Https://github.com/square/okhttp Stetho is a bridge that Facebook has developed to connect Android apps and chrome developer tools: Https://github.com/facebook/stetho Usage: 1. The project dependency package is as follows: Commons-cli-1.2.jarokhttp-2.3.0.jarokio-1.3.0.jarstetho-1.0.1.jarstetho-okhttp-1.0.1.jar 2. You need to inherit the application class to initialize the Stetho tool.
PackageCom.peiandsky.chromedebug;Importandroid.app.Application;ImportCom.facebook.stetho.Stetho; Public classAppextendsApplication {@Override Public voidonCreate () {Super. OnCreate (); Stetho.initialize (Stetho. Newinitializerbuilder ( This). Enabledumpapp (Stetho.defaultdumperpluginsprovider ( This). Enablewebkitinspector (Stetho.defaultinspectormodulesprovider ( This)) . Build ()); }}
The androidmanifest.xml needs to be configured as a app:3 for the program. The code accessed using Okhttp is as follows:
PackageCom.peiandsky.chromedebug;Importjava.io.IOException;ImportCom.facebook.stetho.okhttp.StethoInterceptor;Importcom.squareup.okhttp.OkHttpClient;Importcom.squareup.okhttp.Request;ImportCom.squareup.okhttp.Response; Public classNet {Private Static Final Booleandebug =true; Private StaticOkhttpclient okhttpclient =Newokhttpclient (); Static { if(Debug) {okhttpclient.networkinterceptors (). Add (Newstethointerceptor ()); } } Public Static Final voidAskbaidu () {Request Request=NewRequest.builder (). URL ("http://www.baidu.com"). build (); Try{Response Response=Okhttpclient.newcall (Request). Execute (); String Reslut=response.body (). String (); } Catch(IOException e) {e.printstacktrace (); } }}
After running the program, you will find that the URL bar in Chrome is entered:
chrome://inspect/You can see the connection to the blue inspect, as well as the debug screen at the beginning of this article.
Okhttp+stetho+chrome Debugging Android Network section (original)