Hide chapters

Android Debugging by Tutorials

1 min

Section I: Debugging Basics

Section 1: 8 chapters
Show chapters Hide chapters

11. Profile Network Activity
Written by Zahidur Rahman Faisal

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

You’ve previously learned how to look into memory footprint using the Android Studio Memory Profiler as a part of your debugging process. In this chapter, you’ll continue your quest and learn to inspect network traffic using the Network Inspector bundled with Android Studio.

Your smartphone performs plenty of network operations every day. From uploading a selfie to your social media account to chatting with someone, all of these tasks are some sort of network operation. Smartphone apps like PodPlay let you subscribe to your favorite podcasts and download the latest updates from your subscribed channel, making network calls, all for your convenience.

This is cool but can be overwhelming for your device if the app performs frequent, unnecessary network operations. To minimize your network data usage and resource consumption, you need to inspect your network activity to keep it optimal.

In this chapter, you’ll learn how to:

  • List the network operations your app performs over a timeline.
  • Find states of a network operation from the aspect of connection status or threading.
  • View details of network requests and responses.

The Network Inspector

The Network Inspector is your one-stop solution to spy on network operations executed by PodPlay. It lets you examine how and when the app transferred data with a timeline in real time.

Follow these steps to bring up the Network Inspector:

  1. Open the Podplay starter project and run the app on an emulator or connected device using API level 26 or higher.
  2. Select View ▸ Tool Windows ▸ App Inspection from the menu bar.

  1. In the App Inspection toolbar, choose your device. Then, select com.yourcompany.podplay from the running app process in the dropdown menu.

  1. Select Network Inspector from the tabs.

You won’t see much at this point apart from a blank timeline moving at the bottom of the Network Inspector panel. That’s your network timeline, and soon you’ll be able to make the most out of it.

Using the Network Timeline

The network timeline is a real-time representation of network activities from your app. It starts from the moment you launch the app and moves as time passes. To see it in action, search for a podcast channel following these steps:

Connection View

The Connection View tab displays files or network requests sent or received during the selected period in the network timeline, irrespective of threads. You can inspect each request’s name or query, size, type, status and transmission duration.

Thread View

The Thread View lists all CPU threads that have initiated a network activity from your app. Switch the tab from Connection View to Thread View, and you’ll see a panel like this one below:

Inspecting Details

The Network Inspector offers even more details about the Request, Response and Threads for each individual network operation.


Switch back to the Connection View tab, then select the first row. You’ll see a new panel on the right side:


Now, switch to the Response tab. The Response tab presents the response headers and the full response body. The response headers hold additional information about the server or how the response is cached:


In the Request tab, you don’t get as many details as in the Response tab. This tab has two sections: Application Headers and Body.

Call Stack

The fourth and final tab in the network details panel is Call Stack. Switch to that tab, and you’ll see a stack of threads that corresponds to the network operation as follows:

Tracing Unusual Network Traffic

Now you know how to inspect network operations using the tool-set offered by Network Inspector. Relaunch the PodPlay app, hold on and do nothing for 10 - 20 seconds, and you’ll notice something odd in the network timeline:

Logging Network Operations

Open ItunesService.kt and add these imports on top:

import com.yourcompany.podplay.BuildConfig
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import java.util.concurrent.TimeUnit
companion object {
 val instance: ItunesService by lazy {
   // 1
   val client = OkHttpClient().newBuilder()
       .connectTimeout(30, TimeUnit.SECONDS)
       .writeTimeout(30, TimeUnit.SECONDS)
       .readTimeout(30, TimeUnit.SECONDS)
   // 2
   if (BuildConfig.DEBUG) {
       val interceptor = HttpLoggingInterceptor()
       interceptor.level = HttpLoggingInterceptor.Level.BODY
   // 3
   val retrofit = Retrofit.Builder()

Key Points

  • Network Inspector offers a set of tools showing all the details available on network operations.
  • Network timeline is the key to looking for network activity at any point since you launch the app.
  • You can leverage Connection View and Thread View to look into different aspects of a network operation.
  • Logging HTTP request and response data helps if you still can’t find enough information through Network Inspector.

Where to Go From Here?

Your debugging skills have reached the next level as you’ve mastered the art of debugging network operations! To learn more on this topic, check out the tutorials and videos below:

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2023 Kodeco Inc.

You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.

Unlock now