Sign in Go Pro

RetroFit2

Setting up Retrofit 2 and Issuing a HTTP GET Request

This lesson is for PRO members.

Upgrade today to get access to all the PRO lessons.

Unlock this lesson
Autoplay

Up next

About

In this Lesson you will learn:

  • How to set up Retrofit 2
  • Create a standalone Retrofit instance
  • Send a simple GET request
  • Enqueue a request for asynchronous communication

Links
- Retrofit Documentation

Comments

I am getting an Error while Setting up Retrofit:
Failed to resolve: com.squareup.retrofit2:convertor-gson:2.4.0

I have just added these 2 dependencies

// REST Interface
implementation 'com.squareup.retrofit2:retrofit:2.4.0'

//JSON Parsing
implementation 'com.squareup.retrofit2:convertor-gson:2.4.0'

plz help......

There's a typo: should be converter, not convertor.

Thanks.....

Hello Annyce.

I am trying to run the sample code, but it is not working. Could you give me a hand here, please?

There is an error appearing in the "Build" tab: Manifest merger failed with multiple errors, see logs.

Here is the content of my app/build.gradle:

```
apply plugin: 'com.android.application'

android {
compileSdkVersion 28

defaultConfig {
applicationId "info.adavis.adeptandroid"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
compileOptions {
sourceCompatibility JavaVersion.VERSION18
targetCompatibility JavaVersion.VERSION18
}
}

ext {
supportLibraryVersion = '28.0.0'
retrofitVersion = '2.5.0'
}

dependencies {
// Support Libraries
// implementation "com.android.support:appcompat-v7:$supportLibraryVersion"
implementation "androidx.appcompat:appcompat:1.0.0"
implementation "com.android.support:recyclerview-v7:$supportLibraryVersion"
implementation "com.android.support:design:$supportLibraryVersion"

// REST Interactions
implementation ("com.squareup.retrofit2:retrofit:$retrofitVersion")

// JSON Parsing
implementation 'com.google.code.gson:gson:2.6.1'
implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"

// View Injection
// implementation 'com.jakewharton:butterknife:7.0.1'
implementation 'com.jakewharton:butterknife:10.1.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'

// Logging
implementation 'com.jakewharton.timber:timber:3.1.0'

// Events
implementation 'de.greenrobot:eventbus:2.4.0'

// Image Loading
implementation 'com.squareup.picasso:picasso:2.5.2'

// Testing
testImplementation 'junit:junit:4.12'
}
```

PS: I don't know why the Markdown with ``` it is not working. Anyway...

Continuing, here is the content of my build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
mavenCentral()
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'

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

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

task clean(type: Delete) {
delete rootProject.buildDir
}

Another file I updated is the gradle-wrapper.properties:

Sun Apr 17 17:50:31 EDT 2016

distributionBase=GRADLEUSERHOME
distributionPath=wrapper/dists
zipStoreBase=GRADLEUSERHOME
zipStorePath=wrapper/dists
distributionUrl=https://services.gradle.org/distributions/gradle-5.1.1-all.zip

Hi Rodrigo,

I've updated the branch with a few of the latest dependencies and I was able to build the app. Please try pulling the retrofit2 branch again => https://github.com/adavis/adept-android/tree/retrofit2. You may need to clean the project and/or invalidate caches and restart in Android Studio. I hope that helps!

Unfortunately I still can't run this project. See:

http://bit.ly/2WBSalm

I tried today again, with no success.
Unfortunately, I am stucked.

>
You need to go PRO to post comments.

Lessons in RetroFit2