
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.
1 |
<uses-permission android:name="android.permission.INTERNET" /> |
Create activity_main.xml File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.bhaumik.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> </RelativeLayout> |
Create MainActivity.java File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
package com.example.bhaumik; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String url = "http://samples.openweathermap.org/data/2.5/weather?id=2172797&appid=b1b15e88fa797225412429c1c50c122a1"; OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(url).build(); Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { parseData(response.body().string()); } }); } private void parseData(String string) { try { JSONObject object = new JSONObject(string); JSONObject coord = object.getJSONObject("coord"); double longitude = coord.getDouble("lon"); double latitude = coord.getDouble("lat"); Log.d("LogX","Values :-> " + longitude + " " + latitude); JSONArray jsonArray =object.getJSONArray("weather"); for(int i=0; i<jsonArray.length(); i++) { JSONObject objects = jsonArray.getJSONObject(i); int id = objects.getInt("id"); String main = objects.getString("main"); String desc = objects.getString("description"); String icon = objects.getString("icon"); Log.d("LogX","Values :-> " + id + " " + main + " " + desc + " " + icon); } } catch (JSONException e) { e.printStackTrace(); } } } |
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.