OkHttp

The sentry-android-okhttp library provides OkHttp support for Sentry via the OkHttp Interceptor. The source can be found on GitHub.

On this page, we get you up and running with Sentry's OkHttp Integration, so that it will automatically add a breadcrumb and start a span out of the active span bound to the scope for each HTTP Request.

Install

Sentry captures data by adding an OkHttp Interceptor. To add the OkHttp integration, initialize the Android SDK, then add the sentry-android-okhttp dependency. Using Gradle:

Copied
implementation 'io.sentry:sentry-android:4.4.0-alpha.1'
implementation 'io.sentry:sentry-android-okhttp:4.4.0-alpha.1'

Configure

Configuration should happen once you create your OkHttpClient instance.

Copied
import okhttp3.OkHttpClient
import io.sentry.android.okhttp.SentryOkHttpInterceptor

private val client = OkHttpClient.Builder()
  .addInterceptor(SentryOkHttpInterceptor())
  .build()

Verify

This snippet includes a HTTP Request and captures an intentional message, so you can test that everything is working as soon as you set it up:

Copied
import io.sentry.android.okhttp.SentryOkHttpInterceptor
import io.sentry.Sentry
import java.io.IOException
import okhttp3.OkHttpClient
import okhttp3.Request

@Throws(IOException::class)
fun run(url: String): String? {
  val request = Request.Builder()
    .url(url)
    .build()

  val bodyStr = client
    .newCall(request)
    .execute()
    .body?.toString()

  Sentry.captureMessage("The Message $bodyStr")

  return bodyStr
}

To view and resolve the recorded message, log into sentry.io and open your project. Clicking on the error's title will open a page where you can see detailed information and mark it as resolved.

You can edit this page on GitHub.