Advertisement
Guest User

RetrofitClient.kt

a guest
Sep 15th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.72 KB | None | 0 0
  1. package com.joaomartins.srodkitrwale
  2.  
  3. import android.app.Application
  4. import android.util.Base64
  5. import okhttp3.Interceptor
  6. import retrofit2.Retrofit
  7. import retrofit2.converter.gson.GsonConverterFactory
  8. import okhttp3.OkHttpClient
  9. import okhttp3.Request
  10. import okhttp3.Response
  11. import okhttp3.logging.HttpLoggingInterceptor
  12.  
  13.  
  14.  
  15.     val cred = username + ":" + password
  16.     val AUTH = Base64.encodeToString(cred.getBytes(), Base64.DEFAULT).replace("\n", "")
  17.  
  18.  
  19. //val AUTH2 = java.util.Base64.getEncoder().encode((username + ":" + password).toByteArray()).toString(Charsets.UTF_8)
  20.  
  21.  
  22. class RetrofitClient {
  23.     // Initializing Retrofit
  24.     fun init() : Retrofit{
  25.  
  26.         // Creating the instance of an Interceptor
  27.         val logging = HttpLoggingInterceptor()
  28.         logging.level = HttpLoggingInterceptor.Level.BODY
  29.  
  30.  
  31.         // Creating the OkHttp Builder
  32.         val client = OkHttpClient().newBuilder()
  33.  
  34.  
  35.         // Creating the custom Interceptor with Headers
  36.         val interceptor = Interceptor { chain ->
  37.             val request = chain?.request()?.newBuilder()?.addHeader("Authorization", AUTH)?.build()
  38.             chain?.proceed(request)
  39.         }
  40.         client.addInterceptor(interceptor) // Attaching the Interceptor
  41.  
  42.  
  43.  
  44.         // Creating the instance of a Builder
  45.         return Retrofit.Builder()
  46.                 .baseUrl("https://srodki.herokuapp.com/")   // The API server
  47.                 .client(client.build())                                     // Adding Http Client
  48.                 .addConverterFactory(GsonConverterFactory.create()) // Object Converter
  49.                 .build()
  50.     }
  51.  
  52.     fun providesGetLogin(retrofit: Retrofit): GET_LOGIN = retrofit.create(GET_LOGIN::class.java)
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement