Okhttp+stetho+chrome Debugging Android Network section (original)

Source: Internet
Author: User
Tags chrome developer chrome developer tools

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.