Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.joaomartins.srodkitrwale
- import android.app.Application
- import android.util.Base64
- import okhttp3.Interceptor
- import retrofit2.Retrofit
- import retrofit2.converter.gson.GsonConverterFactory
- import okhttp3.OkHttpClient
- import okhttp3.Request
- import okhttp3.Response
- import okhttp3.logging.HttpLoggingInterceptor
- val cred = username + ":" + password
- val AUTH = Base64.encodeToString(cred.getBytes(), Base64.DEFAULT).replace("\n", "")
- //val AUTH2 = java.util.Base64.getEncoder().encode((username + ":" + password).toByteArray()).toString(Charsets.UTF_8)
- class RetrofitClient {
- // Initializing Retrofit
- fun init() : Retrofit{
- // Creating the instance of an Interceptor
- val logging = HttpLoggingInterceptor()
- logging.level = HttpLoggingInterceptor.Level.BODY
- // Creating the OkHttp Builder
- val client = OkHttpClient().newBuilder()
- // Creating the custom Interceptor with Headers
- val interceptor = Interceptor { chain ->
- val request = chain?.request()?.newBuilder()?.addHeader("Authorization", AUTH)?.build()
- chain?.proceed(request)
- }
- client.addInterceptor(interceptor) // Attaching the Interceptor
- // Creating the instance of a Builder
- return Retrofit.Builder()
- .baseUrl("https://srodki.herokuapp.com/") // The API server
- .client(client.build()) // Adding Http Client
- .addConverterFactory(GsonConverterFactory.create()) // Object Converter
- .build()
- }
- fun providesGetLogin(retrofit: Retrofit): GET_LOGIN = retrofit.create(GET_LOGIN::class.java)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement