Android Profiler请求体为空:升级AS3.1后日志异常求助
Hey there, I’ve dealt with this exact frustration before—upgrading to Android Studio 3.1+ and suddenly losing the ability to see full request bodies in the Profiler instead of the old Android Monitor. Let’s walk through the fixes that worked for me:
1. Double-Check Your OkHttp Logging Interceptor Setup
The Profiler relies on OkHttp's logging interceptor to capture request details, so make sure this is configured correctly:
- First, ensure you have the logging interceptor dependency in your
build.gradle(Module level):implementation 'com.squareup.okhttp3:logging-interceptor:4.11.0' // Use the latest compatible version - Create the interceptor with BODY-level logging—this is critical, since lower levels won’t capture the full request body:
val loggingInterceptor = HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BODY } - Add it as an application interceptor (not a network interceptor) to your OkHttp client. Application interceptors run early in the request chain, so they capture the full, unmodified request body before any redirects or network-level tweaks:
val okHttpClient = OkHttpClient.Builder() .addInterceptor(loggingInterceptor) // Use addInterceptor, not addNetworkInterceptor .build()
2. Don’t Consume the Request Body in Custom Interceptors
If you have custom interceptors (like for auth signing or request modification), you might be accidentally "using up" the request body—OkHttp RequestBody is a one-time stream by default. To fix this:
- Wrap the request body in a
BufferedRequestBodybefore reading it, then reset the buffer so the Profiler can still access it:override fun intercept(chain: Interceptor.Chain): Response { val originalRequest = chain.request() val requestBody = originalRequest.body if (requestBody != null) { val bufferedBody = requestBody.buffer() // Read the body content for your custom logic val bodyContent = bufferedBody.readUtf8() // Reset the buffer so subsequent interceptors (including Profiler) can read it again bufferedBody.reset() } // Proceed with the modified or original request return chain.proceed(originalRequest) }
3. Verify Profiler Settings
Sometimes the issue is just a misconfigured Profiler:
- Open the Network Profiler tab and make sure you’ve selected your app’s active process from the dropdown at the top.
- Click the gear icon (Settings) in the top-left corner of the Network tab, and confirm Enable OkHttp tracing is checked.
- If it’s still not working, try restarting Android Studio and your emulator/physical device—Profiler can sometimes get stuck in a cached state.
4. Ensure Retrofit Uses Your Configured OkHttpClient
If you’re using Retrofit, don’t forget to pass your properly configured OkHttp client to the Retrofit builder:
val retrofit = Retrofit.Builder() .baseUrl("YOUR_BASE_URL") .client(okHttpClient) // This links your logging setup to Retrofit .addConverterFactory(GsonConverterFactory.create()) .build()
After going through these steps, you should start seeing the full request body in the Profiler’s Request tab alongside the response.
内容的提问来源于stack exchange,提问作者shehzy

