Knowing All about Okhttp in android

Glitch
2 min readJun 23, 2021

--

What is Okhttp

OkHttp is a third-party library developed by Square for sending and receive HTTP-based network requests. It is built on top of the Okio library, which tries to be more efficient about reading and writing data than the standard Java I/O libraries by creating a shared memory pool. … URL, java. net. URI, or android.

What is the use of OkHttp in Android?

OkHttp Overview. OkHttp is an efficient HTTP & HTTP/2 client for Android and Java applications. It comes with advanced features such as connection pooling (if HTTP/2 isn’t available), transparent GZIP compression, and response caching to avoid the network completely for repeated requests.

What is the difference between retrofit and OkHttp?

Retrofit is a REST Client for Java and Android. … OkHttp is a pure HTTP/SPDY client responsible for any low-level network operations, caching, requests, and response manipulation. In contrast, Retrofit is a high-level REST abstraction build on top of OkHttp.

How is OkHttp implemented?

OkHttp Query Parameters Example

Builder urlBuilder = HttpUrl. parse(“https://httpbin.org/get).newBuilder(); urlBuilder. addQueryParameter(“website”, “www.journaldev.com"); urlBuilder. addQueryParameter(“tutorials”, “android”); String url = urlBuilder.

OkHttp Android Advantages

Some advantages that OkHttp brings to us are:

  1. Connection pooling
  2. Gziping
  3. Caching
  4. Recovering from network problems
  5. Redirects
  6. Retries
  7. Support for synchronous and asynchronous calls

Let’s Code

Before we look into the implementation of OkHttp Android, add the following dependency

compile 'com.squareup.okhttp3:okhttps:3.4.1'

Add the permission for internet inside the AndroidManifest.xml file.

<uses-permission android:name=”android.permission.INTERNET”/>

Okhttp to post

val payload = "test payload"

val okHttpClient = OkHttpClient()
val requestBody = payload.toRequestBody()
val request = Request.Builder()
.method("POST", requestBody)
.url("url")
.build()
okHttpClient.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
// Handle this
}

override fun onResponse(call: Call, response: Response) {
// Handle this
}
})

Don't forget to import:

import okhttp3.RequestBody.Companion.toRequestBody

JSON Get Request

val BASE_URL = "https:yoururl"
val ACCESS_KEY = "..."
val path = "/photos/$id"
val uri = Uri.parse(BASE_URL)
.buildUpon()
.appendEncodedPath(path)
//.appendPath(path)
.build()
val client = OkHttpClient()
val request = Request.Builder()
.url(uri.toString())
.addHeader("Accept-Version", "v1")
.addHeader("Authorization", "Client-ID $ACCESS_KEY")
.get()
.build()
val response = client.newCall(request).execute()
val jsonDataString = response.body()?.string()
val json = JSONObject(jsonDataString)
if (!response.isSuccessful) {
val errors = json.getJSONArray("errors").join(", ")
throw Exception(errors)
}
val rawUrl = json.getJSONObject("urls").getString("raw")

Ta-da !! the above is just a simple example to get and post using okhttp hope that helps . Keep Coding!!

--

--

No responses yet