Retrofit

From bibbleWiki
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()

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