Setting up OkHttp for network requests in an Android application

TRUETECH is engaged in the development, support and maintenance of iOS, Android, PWA mobile applications. We have extensive experience and expertise in publishing mobile applications in popular markets like Google Play, App Store, Amazon, AppGallery and others.
Development and support of all types of mobile applications:
Information and entertainment mobile applications
News apps, games, reference guides, online catalogs, weather apps, fitness and health apps, travel apps, educational apps, social networks and messengers, quizzes, blogs and podcasts, forums, aggregators
E-commerce mobile applications
Online stores, B2B apps, marketplaces, online exchanges, cashback services, exchanges, dropshipping platforms, loyalty programs, food and goods delivery, payment systems.
Business process management mobile applications
CRM systems, ERP systems, project management, sales team tools, financial management, production management, logistics and delivery management, HR management, data monitoring systems
Electronic services mobile applications
Classified ads platforms, online schools, online cinemas, electronic service platforms, cashback platforms, video hosting, thematic portals, online booking and scheduling platforms, online trading platforms

These are just some of the types of mobile applications we work with, and each of them may have its own specific features and functionality, tailored to the specific needs and goals of the client.

Showing 1 of 1 servicesAll 1735 services
Setting up OkHttp for network requests in an Android application
Medium
from 1 business day to 3 business days
FAQ
Our competencies:
Development stages
Latest works
  • image_mobile-applications_feedme_467_0.webp
    Development of a mobile application for FEEDME
    756
  • image_mobile-applications_xoomer_471_0.webp
    Development of a mobile application for XOOMER
    624
  • image_mobile-applications_rhl_428_0.webp
    Development of a mobile application for RHL
    1052
  • image_mobile-applications_zippy_411_0.webp
    Development of a mobile application for ZIPPY
    947
  • image_mobile-applications_affhome_429_0.webp
    Development of a mobile application for Affhome
    862
  • image_mobile-applications_flavors_409_0.webp
    Development of a mobile application for the FLAVORS company
    445

Setting up OkHttp for Network Requests in Android Application

OkHttp is the HTTP client on which Retrofit, Coil, Glide and most Android libraries working with network are built. Use OkHttp directly when you need full control: WebSocket connections, custom protocols, file uploads with progress, specific header handling — things either impossible in Retrofit or requiring workarounds.

When OkHttp Directly, Not Through Retrofit

WebSocket: OkHttpClient.newWebSocket(request, listener) — native support without extra dependencies. WebSocketListener receives callbacks onOpen, onMessage, onFailure, onClosed. For automatic reconnect you need own logic with exponential backoff.

File upload/download with progress. Retrofit allows uploading via @Multipart, but tracking progress only via custom RequestBody wrapping source and calling callback on byte write. This is OkHttp level.

Custom authentication. Authenticator interface OkHttp calls on 401, allows synchronously getting new token and retrying request. Works with Retrofit too, but via OkHttpClient.

Shared HTTP client for multiple libraries. Coil accepts OkHttpClient in ImageLoader.Builder, Retrofit in Retrofit.Builder. One configured client with shared connection pool and cache instead of several separate clients with duplicated logic.

OkHttpClient Configuration

val okHttpClient = OkHttpClient.Builder()
    .connectTimeout(30, TimeUnit.SECONDS)
    .readTimeout(30, TimeUnit.SECONDS)
    .writeTimeout(30, TimeUnit.SECONDS)
    .cache(Cache(cacheDir, 10 * 1024 * 1024)) // 10 MB cache
    .addInterceptor(authInterceptor)
    .addInterceptor(loggingInterceptor)
    .addNetworkInterceptor(networkMonitorInterceptor)
    .authenticator(tokenRefreshAuthenticator)
    .connectionPool(ConnectionPool(5, 5, TimeUnit.MINUTES))
    .build()

addInterceptor vs addNetworkInterceptor: application-interceptors called once and see cached responses. Network-interceptors only for real network requests. For logging byte traffic — network interceptor. For adding auth headers — application interceptor.

HTTP cache: Cache with correct cache-dir (usually context.cacheDir) speeds up repeat requests and works offline if server sends Cache-Control headers. If not — ForceCacheInterceptor with forced FORCE_CACHE for offline.

Certificate pinning: CertificatePinner.Builder().add("api.example.com", "sha256/AAAA...").build(). SHA-256 fingerprint obtained from cert via openssl x509 -in cert.pem -pubkey -noout | openssl pkey -pubin -outform der | openssl dgst -sha256 -binary | base64. Pinning breaks on certificate rotation — must add new fingerprint before change.

Typical Mistakes

Creating OkHttpClient on every request. Client holds thread pool, connection pool and cache — must be singleton. In Hilt — @Singleton in @Provides.

Blocking operations inside Interceptor. Interceptor.intercept() runs on OkHttp dispatcher threads, but if doing suspend function via runBlocking — blocks dispatcher thread. For async operations in interceptor (e.g., token refresh) use synchronized block with condition variable, or move refresh to Authenticator which is synchronous by contract.

Testing: MockWebServer from com.squareup.okhttp3:mockwebserver — standard tool for network layer unit-tests. Starts local server, accepts requests, returns prepared responses.

OkHttp setup with interceptors, cache, WebSocket or file uploads — 1–3 days. Cost calculated individually.