Friday, December 31, 2021

Android Kotlin Web Services With Retrofit PART 2

 Android Kotlin Web Services With Retrofit PART 2

Retrofit Library

All External libraries or third party libraries(Retrofit) are like extensions to the core Android APIs. They are mostly open source, community-developed, and maintained by the collective contributions from the huge android community around the world. This enables Android developers like you to build better apps.

The External Retrofit library that you are going to use in this blog to talk to the RESTful web service is a good example of a well-supported and maintained library. You can tell this by looking at its GitHubRetrofit page, checkout the open issues and closed issues. If the developers are resolving the issues and responding to the feature requests on a regular basis, then it implies that this library is well maintained and is a good candidate to use in the app.

Retrofit library will communicate with the backend. It creates URI's for the web service based on the parameters we pass to it. You will see more on this in later sections.

Add Retrofit dependencies

Android Gradle allows you to add external libraries to your project. In addition to the library dependency, you should also include the repository where the library is hosted. The Google libraries such as ViewModel and LiveData from the Jetpack library are hosted in the Google repository. The majority of community libraries are hosted on JCenter like Retrofit.

  1. Open the project's top-level level build.gradle(Project) file. Notice the repositories listed under the repositories block. You should see two repositories, google()mavenCentral().
repositories {
   google
()
   mavenCentral
()
}

  1. Open module level gradle file, build.gradle (Module: MarsPhots.app).
  2. In the dependencies section, add these lines for the Retrofit libraries:
// Retrofit 
implementation
"com.squareup.retrofit2:retrofit:2.9.0"
// Retrofit with Moshi Converter
implementation
"com.squareup.retrofit2:converter-scalars:2.9.0"

The first dependency is for the Retrofit2 library itself, and the second dependency is for the Retrofit scalar converter. This converter enables Retrofit to return the JSON result as a String. The two libraries work together.

  1. Click Sync Now to rebuild the project with the new dependencies.

Add support for Java 8 language features

  1. To use the built-in features, you need the following code in your module's build.gradle file. This step is already done for you, make sure the following code is present in your build.gradle(Module: MarsPhotos.app).
android {
  ...
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility

    targetCompatibility JavaVersion.VERSION_1_8
 

  }
 
  kotlinOptions {
    jvmTarget = '1.8'
  }
}

Many third party libraries including Retrofit2 use Java 8 language features. The Android Gradle plugin provides built-in support for using certain Java 8 language features.



Thursday, December 30, 2021

Android Kotlin Web Services With Retrofit PART 1

Kotlin Android Webservice - PART 1

All the data is stored on a web server. To get this data into your app you need to establish a connection and communicate with the server on the internet.



Most of the web servers today run web services using a common state-less web architecture known as REST, which stands for REpresentational State Transfer. Web services that offer this architecture are known as RESTful services.

Requests are made to RESTful web services in a standardized way via URIs. An URI (Uniform Resource Identifier) identifies a resource in the server by name, without implying its location or how to access it. For example, in the app for this lesson, you retrieve the image urls using the following server URI (This server hosts all Data Required):

A Uniform Resource Locator (URL) is a URI that specifies the means of acting upon or obtaining the representation of a resource, It specifying both its primary access mechanism and network location.

Sending Web service request

Here Each web service request contains a URI, and it is transferred to the server using the same HTTP protocol that's used by web browsers, like Chrome. HTTP requests contain an operation to tell the server what to do.

Common HTTP operations include:

  • GET for retrieving server data
  • POST or PUT for add/create/update the server with new data
  • DELETE for deleting data from the server

Your app will make an HTTP GET request to the server for the Mars photos information, and then the server returns a response to our app including image urls.


The response from a web service is commonly formatted in one of the common web formats like XML or JSON – formats for representing structured data in key-value pairs. You learn more about JSON in the later task.

In this task, you establish a network connection to the server, communicate with the server, and receive a JSON response. And we will be using a backend server that is already written for you. In this Part we will use the Retrofit library, a third party library to communicate with the backend server as shown in Picture.

Catch Up PART 2


Android Kotlin Web Services With Retrofit PART 2

 Android Kotlin Web Services With Retrofit PART 2 Retrofit Library All External libraries or third party libraries(Retrofit) are like extens...