Retrofit

From bibbleWiki
Revision as of 11:52, 24 January 2021 by Iwiseman (talk | contribs) (Combining)
Jump to navigation Jump to search

Resource

During the course there were demo server APIs
https://github.com/alex-wolf-ps/RetrofitAPINode
https://github.com/alex-wolf-ps/RetrofitAPIDotnet
https://github.com/alex-wolf-ps/AndroidGlobomanticsApp

Getting Started

Permissions

We need to allow apps to use the internet so we need to add permissions to the manifest.

<uses-permission android:name="android.permission.INTERNET" />

Add Retrofit and Gson to Gradle

In the app gradle add

    implementation group: 'com.squareup.retrofit2', name: 'retrofit', version: '2.8.0'
    implementation group: 'com.squareup.retrofit2', name: 'converter-gson', version: '2.9.0'

Making a Request

Introduction

To do this we need to do three steps

Step 1 Define the interface

interface ContractService {
    @GET("contacts")
    Call<List<Idea>> getContacts()

    @POST("contract")
    Call<Idea> createContract(@Body Contact contact)

    @PUT("contracts")
    Call<Idea> updateContract(@Body Contact contact)

    @DELETE("contracts")
    Call<Idea> updateContract(@Path("id") int contactId)
}

Step 2 Configuring and Building the Service

We need to create a Build and Retrofit class

  • Build Class, defines the configurations
  • Retrofit Class, creates instances of the service

Step 3 Send And Receive

We can then use the service. For this we need to implement a

  • Success Handler
  • Error Handler
ideaService.getIdeas()

Worked Example

Here is a worked example

Step 1 Define the interface

public interface MessageService{
    @GET("messages")
    fun getMessages(): Call<String>
}

Step 2 Configuring and Building the Service

    private const val URL = "http://10.0.2.2:9000/"

    private val builder = Retrofit.Builder().baseUrl(URL)
            .addConverterFactory(GsonConverterFactory.create())

    private val retrofit = builder.build()

    fun <S> buildService(serviceType: Class<S>?): S {
        return retrofit.create(serviceType)
    }


I use genymotion for speed when doing x86 projects so the local host for me was 192.168.56.1, and obtained using

ip a |grep vb

Step 3 Send And Receive

Within the activity we

  • create an instance to the service.
  • use the service
  • handle the response and failure
        val taskService = buildService(MessageService::class.java)
        val call = taskService.getMessages()
        call.enqueue(object : Callback<String?> {
            override fun onResponse(request: Call<String?>, response: Response<String?>) {
                (findViewById<View>(R.id.message) as TextView).text = response.body()
            }

            override fun onFailure(request: Call<String?>, t: Throwable) {
                (findViewById<View>(R.id.message) as TextView).text = "Request Failed"
            }
        })

Adding Logging

Cannot believe the demo suggested logging can be a dry subject. Has to be the most important part of using new frameworks. Without the logging it is impossible to progress. Here goes - sorry about the rant.
For gradle we add

    implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'
    implementation("com.squareup.okhttp3:logging-interceptor:4.9.0")


Now like Angular we add the use of the interceptors (UFO)

    private static HttpLoggingInterceptor logger =
            new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);

    // Create OkHttp Client
    private static OkHttpClient.Builder okHttp =
            new OkHttpClient.Builder().addInterceptor(logger);

We need to also add the okHttp to our builder retrofit builder too

    private val builder = Retrofit.Builder().baseUrl(URL)
            .addConverterFactory(GsonConverterFactory.create()).client(okHttp.build())


We can now see useful logs in the logcat

Parameters

Path Parameters

Path Parameters populate URL segment placeholders.

public interface IdeaService {
...
    @GET("ideas/{id}")
    fun getIdea(@Path("id") ideaId: Int): Call<Idea?>
}

Query Parameters

Query Parameters are automatically appended to the end of the URL

public interface IdeaService {
...
    @GET("ideas")
    fun getIdeas(@Query("status") status: String): Call<List<Idea?>?>
}

Combining

We can group these together

    @GET("ideas/{id}")
    fun getIdeas(
          @Path("id") ideaId: Int, 
          @Query("status") status: String): Call<List<Idea?>?>


Or better still we can provide a query map which provides a named list of parameters

    @GET("ideas")
    fun getIdeas(@QueryMap parameters: Map<String, String>): Call<List<Idea?>?>

Example of using the QueryMap Filter

So below is an example filter using a map. Here is the interface

    @GET("ideas")
    fun getIdeas(@QueryMap owner: HashMap<String?, String?>?): Call<List<Idea?>?>?


And here is the code

...
        val filterMap = HashMap<String?, String?>()
        filterMap["owner"] = "Jim"
        filterMap["count"] = "1"
        val ideaService = buildService(IdeaService::class.java)
        val request = ideaService.getIdeas(filterMap)
        request?.enqueue(object : Callback<List<Idea?>?> {
            override fun onResponse(request: Call<List<Idea?>?>, response: Response<List<Idea?>?>) {
                recyclerView.adapter = SimpleItemRecyclerViewAdapter(response.body() as List<Idea>)
            }

            override fun onFailure(request: Call<List<Idea?>?>, t: Throwable) {
                (findViewById<View>(R.id.message) as TextView).text = "Request Failed"
            }
        })

Old Projects

Use Java 8

Sometimes when using older projects with gradle you might see

Couldn't determine java version from '11.0.1'

This is because of an old version of gradle. The easiest way to solve it is to go to the command line and change your JAVA_HOME is 8 e.g.

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64

Gradle 3.3 to 4.10

To achieve this I change the build.gradle to update the plugin and specify the maven and google repositories.

buildscript {
    repositories {
        google()
        jcenter()
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

And ran gradlew not gradle and also updated the gradle.warapper.properties

distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip

From there I could use the standard update to 4.10

./gradlew wrapper --gradle-version 4.10

Gradle 4.10 to 5.4.1

For this I needed to change the plugin in build.gradle to be

   classpath 'com.android.tools.build:gradle:3.5.1'

And of course the gradle.warapper.properties

distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip

I also need to change the SDK build toools in the app to be 26

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "com.example.alexr.ideamanager"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

Next I need to change the following from the 25 version

    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:support-v4:25.3.1'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'

To

    compile 'com.android.support:appcompat-v7:26.0.1'
    compile 'com.android.support:support-v4:26.0.1'
    compile 'com.android.support:recyclerview-v7:26.0.1'
    compile 'com.android.support:design:26.0.1'

Gradle 5.4.1 to 6.x

To do this I started Android Studio where I was prompted to upgrade to 6.x