Skip to content

Instantly share code, notes, and snippets.

@vamjakuldip
Created January 24, 2020 04:21
Show Gist options
  • Save vamjakuldip/c51a9c94f68a30135587dda9036a9e16 to your computer and use it in GitHub Desktop.
Save vamjakuldip/c51a9c94f68a30135587dda9036a9e16 to your computer and use it in GitHub Desktop.
Network Api Service with Retrofit
package com.vk.android.network
import com.vk.android.BuildConfig
import io.reactivex.Observable
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Body
import retrofit2.http.Headers
import retrofit2.http.POST
import java.util.concurrent.TimeUnit
interface ApiService {
@Headers("Content-Type:application/json;charset=UTF-8")
@POST("api/register")
fun signUp(@Body map: HashMap<String, Any?>): Observable<Any>
companion object Factory {
private var apiService: ApiService? = null
private val okHttpClient = OkHttpClient.Builder()
.connectTimeout(31, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
fun create(): ApiService {
if (apiService == null) {
if (BuildConfig.DEBUG) {
val httpLog = HttpLoggingInterceptor()
httpLog.setLevel(HttpLoggingInterceptor.Level.BODY)
okHttpClient.addInterceptor(httpLog)
}
val retrofit = Retrofit.Builder()
.client(okHttpClient.build())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BuildConfig.BASE_URL)
.build()
apiService = retrofit.create(ApiService::class.java);
}
return apiService!!
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment