Skip to content

theanandankit/retrofit-simple-example

Repository files navigation

Retrofit simple example

What is Retrofit

Retrofit is a REST Client for Java and Android. It makes it relatively easy to retrieve and upload JSON (or other structured data) via a REST based webservice. In Retrofit you configure which converter is used for the data serialization. Typically for JSON you use GSon, but you can add custom converters to process XML or other protocols. Retrofit uses the OkHttp library for HTTP requests.

Implementation

Use the Following Library

 implementation 'com.squareup.retrofit2:retrofit:2.5.0'
 implementation 'com.squareup.retrofit2:converter-scalars:2.5.0'
 implementation 'com.squareup.retrofit2:converter-gson:2.5.0'

STEP INVOLVED

Step 1: Create Interface of Retrofit (retrofite/Retrofit_interface).

Step 2: Build the Retrofit and define Base url, serializer etc (retrofite/APIClint).

Step 3: Define the POJO class for the expected Json file (Model/Data).

Step 4: Call the retrofit call method to receive the Response from server.

Useful URL Video

RETROFIT Tutorial (v 2.5.0) | HTTP Client

Retrofit Tutorial Part 1 - SIMPLE GET REQUEST - Android Studio Tutorial

URL Used

In this example we are using the Weather API.( Make sure you subscribe and get your own key )

URL: api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={your api key}

Method : GET

Our Response

Example: api.openweathermap.org/data/2.5/weather?lat=35&lon=139 (Make sure you replace with your key after the subscription LINK)

 {"coord": { "lon": 139,"lat": 35},
  "weather": [
    {
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01n"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 281.52,
    "feels_like": 278.99,
    "temp_min": 280.15,
    "temp_max": 283.71,
    "pressure": 1016,
    "humidity": 93
  },
  "wind": {
    "speed": 0.47,
    "deg": 107.538
  },
  "clouds": {
    "all": 2
  },
  "dt": 1560350192,
  "sys": {
    "type": 3,
    "id": 2019346,
    "message": 0.0065,
    "country": "JP",
    "sunrise": 1560281377,
    "sunset": 1560333478
  },
  "timezone": 32400,
  "id": 1851632,
  "name": "Shuzenji",
  "cod": 200
}

THANK YOU

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages