Android OKHttp Library Json With ListView Tutorial with Example

By | June 18, 2019
OkHttp Library Json with ListView in Android Tutorial Example

What is OkHttp in Android ?

OkHttp is a third party library that was introduced by Square in 2013 for sending and receive HTTP-based network requests.

OkHttp library is a  two HTTP clients: HttpURLConnection and Apache HTTP Client for sending and receiving data from the web. 

OkHttp provides  HttpURLConnection and Apache Client. it is interfaces by working directly on a top of java Socket without using any extra dependencies.

OKHttp library is a used to fetch json data into recyclerView or listView. in this library is a used for server connection to send request and retrieve data from server to recyclerView.

OkHttp is a open source project designed to HTTP client. It is supports the SPDY protocol. SPDY is HTTP 2.0 and allows multiple HTTP requests. it can multiple over one socket connection.

OkHttp is Support for synchronous and asynchronous.

What is Synchronous in Android ?

Synchronous is a require an AsyncTask wrapper. it does not support cancelling a request. it is not preferred. AsyncTask is leak the activity context.

What is Asynchronous in Android ?

Asynchronous is a supports native cancelling request. it is multiple requests and canceling a single method called.

OkHttp library Json Example in Android

Required Internet Permission in AndroidManifest.xml File.

Create activity_main.xml File Follow this Code.

Create MainActivity.java File Follow this Code.

In this Example used to OkHttp library to fetch json data in android.i have create OkHttpClient object and then fetch json data.create simple list and then create OkHttpClient library object and then implement methods to when json is success to create method and then this method is use to JSONObject to create object of json. i have simple weather url to fetch lattitude and longitude data from listView.

Another used to JSONArray object to get the data from json url and then set the list adapter.