Setting up Retrofit

First of all, we will add these dependencies in the build.gradle file to set up Retrofit:

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'

Consider the following line:

compile 'com.squareup.retrofit2:retrofit:2.1.0'

It adds the core Retrofit libraries to the project. Now, consider this:

compile 'com.squareup.retrofit2:converter-gson:2.1.0'

It makes the GsonConverterFactory class available, which is used internally by Retrofit to parse JSON responses.

Consider the record of the following:

compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'

It lets us read HTTP responses using RxJava library and its Observables. We will enable this behavior using the RxJava2CallAdapterFactory class. The following line is used to configure the dependencies for the logging that will be used in Retrofit:

compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'

The first line is the Retrofit library itself, the second line will let us convert JSON responses to our internal datatypes using Gson, and the third line will expose a Retrofit interface in RxJava compatible way. The last line will allow the use of logging that's very useful while developing the app.

Finally, if you are using ProGuard, these lines might be needed:

-dontnote retrofit2.Platform
-dontnote retrofit2.Platform$IOS$MainThreadExecutor
-dontwarn retrofit2.Platform$Java8
-keepattributes Signature
-keepattributes Exceptions

Now we are ready to start making HTTP requests with Retrofit.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.188.244