GET And POST request using OKHttp in Android Application
Aneh Thakur
. 5 min read
In this post I will explain to you how we can send GET And POST request using OKHttp in Android Application. In my last post, I will explain how we can send GET and Post Request using Volley. OKHttp and Http & Http/2 client is widely used in android and java applications to create a request to the server. In this post, I am going to explain some most useful feature of OKHttp.
OKHttp is the modern way to make and exchange data and media over HTTP network. It helps to load data faster, efficiently and also saves bandwidth.
HTTP/2 support allows all requests to the same host to share a socket.
Connection pooling reduces request latency (if HTTP/2 isn’t available).
Transparent GZIP shrinks download sizes.
Response caching avoids the network completely for repeat requests.
OkHttp is very easy to use. Its request/response API is designed with fluent builders and immutability. It supports both synchronous blocking calls and async calls with callbacks. Now we can start creating our android application.
Step 1. Create a new Android project in Android Studio and name it what you like.
Step 2. Now we need to add dependencies to our Android project add below code and sync
Step 3. Add internet permission in manifest file
Now I can create a simple method in which In which I can explain how we can use OkHttp to send the request.
When we call above method it returns a list of the user. This method makes an Asynchronous call to the server because if you make the Synchronous call using Response method it will through NetworkOnMainThread exception on response.body() error so I prefer to use Async call.
In above method, I also add header because in my server API it is required to send header type and it is very easy in OKHttp to set header and Auth Token in it I further explain how to do that. And also to make Async call we need to add newCall() and enqueue() to run and get callback response in it.
Step 4. In this step, I will explain to you how we can post data to the server in OKHttp if you want to send get request please check function in Step 3 I created. To create a post request we need to create a JSON Object and after that, we need to convert JSON to string and post it to the server it was a very easy task.
Above method is same as we create method on step 3 but in this, we create JSON Object and pass that object to RequestBody and the add RequestBody in post method where we create actual request.
Screen Layout Code
Here is MainActivity Code
Note please change username and password when you run this example.
Hope this post helps you.
More Stories from
🚀 How to Build an MCP Server for Zerodha – AI-Powered Trading with Claude
Learn how to build an MCP (Model Context Protocol) server that connects Claude AI with Zerodha’s trading API. Execute stock trades, view portfolios, and automate strategies using natural language.
React Native 0.78 Unveiled: New Features, Changes, and Benefits You’ll Love 🚀
Discover React Native 0.78! From React 19 support to Android vector drawables and better iOS integration, explore the latest features, changes, and benefits with examples to make app development faster and smoother. 🌟
🚀 OpenAI’s $3 Billion Windsurf Acquisition: What It Really Means
OpenAI's $3 billion Windsurf deal shows that developer tools—not chatbots—are the real future of AI. Here’s what this means for coders, jobs, and the evolving dev landscape.
🚀 Encore.ts: Blazing Fast Backend Powerhouse – 9x Faster Than Express.js & 3x Faster Than Bun + Zod
Discover why Encore.ts outshines Express.js and Bun + Zod with 9x and 3x faster performance, respectively. Explore sample code, speed benchmarks, and see how this TypeScript framework redefines backend efficiency! ⚡
Trump Unveils U.S. Crypto Strategic Reserve: Bitcoin and Altcoins Surge
Donald Trump announces a U.S. Crypto Strategic Reserve featuring Bitcoin, Ethereum, XRP, Solana, and Cardano, sparking a $300B market rally. Explore the implications and trends as of March 3, 2025.