Skip to content

Instantly share code, notes, and snippets.

@guangtuan
Last active April 15, 2018 07:49
Show Gist options
  • Save guangtuan/a39a1449251526574a207fd3c1b9a030 to your computer and use it in GitHub Desktop.
Save guangtuan/a39a1449251526574a207fd3c1b9a030 to your computer and use it in GitHub Desktop.
retrofix rxjava gson start-kit
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 27
defaultConfig {
minSdkVersion 14
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
ext.retrofitsVer = '2.4.0'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation 'io.reactivex.rxjava2:rxjava:2.1.12'
implementation 'com.google.code.gson:gson:2.8.2'
implementation "com.squareup.retrofit2:retrofit:$retrofitsVer"
implementation "com.squareup.retrofit2:converter-gson:$retrofitsVer"
implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofitsVer"
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
repositories {
mavenCentral()
}
package tech.igrant.rxjavaretrofitgson
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import java.util.HashMap
class RetrofitFacade private constructor() {
companion object {
@Volatile
private var retrofitMap: MutableMap<String, Retrofit> = HashMap()
private val converter: GsonConverterFactory = GsonConverterFactory.create()
private val callAdapterFactory: RxJava2CallAdapterFactory = RxJava2CallAdapterFactory.create()
fun get(url: String): Retrofit {
synchronized(retrofitMap, {
if (!retrofitMap.containsKey(url)) {
retrofitMap[url] = Retrofit.Builder().baseUrl(url).addConverterFactory(converter).addCallAdapterFactory(callAdapterFactory).build()
}
return retrofitMap[url]!!
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment