Guest User

Untitled

a guest
Sep 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. package com.joaomartins.srodkitrwale
  2.  
  3. import okhttp3.OkHttpClient
  4. import retrofit2.Call
  5. import retrofit2.Retrofit
  6. import retrofit2.converter.gson.GsonConverterFactory
  7. import retrofit2.http.GET
  8. import retrofit2.http.Header
  9. import retrofit2.http.Headers
  10.  
  11. interface GET_LOGIN {
  12.  
  13.  
  14. @GET("login")
  15. fun getAccessToken() : Call<String>
  16. }
  17.  
  18. package com.joaomartins.srodkitrwale
  19.  
  20. import android.app.Application
  21. import android.util.Base64
  22. import kotlinx.android.synthetic.main.activity_login.*
  23. import okhttp3.Interceptor
  24. import retrofit2.Retrofit
  25. import retrofit2.converter.gson.GsonConverterFactory
  26. import okhttp3.OkHttpClient
  27. import okhttp3.Request
  28. import okhttp3.Response
  29. import okhttp3.logging.HttpLoggingInterceptor
  30.  
  31. val username = Login().userTxt.text
  32. val password = Login().passTxt.text
  33. val credentials = username + ":" + password
  34. val AUTH = "Basic " + Base64.encodeToString(credentials.toByteArray(Charsets.UTF_8), Base64.DEFAULT).replace("n", "")
  35.  
  36.  
  37.  
  38.  
  39.  
  40. val retrofit = RetrofitClient().init()
  41. //.getBytes(), Base64.DEFAULT).replace("n", "")
  42. //val AUTH2 = java.util.Base64.getEncoder().encode((username + ":" + password).toByteArray()).toString(Charsets.UTF_8)
  43.  
  44. class RetrofitClient {
  45. // Initializing Retrofit
  46. fun init() : Retrofit{
  47.  
  48. // Creating the instance of an Interceptor
  49. val logging = HttpLoggingInterceptor()
  50. logging.level = HttpLoggingInterceptor.Level.BODY
  51.  
  52.  
  53. // Creating the OkHttp Builder
  54. val client = OkHttpClient().newBuilder()
  55.  
  56.  
  57. // Creating the custom Interceptor with Headers
  58. val interceptor = Interceptor { chain ->
  59. val request = chain?.request()?.newBuilder()?.addHeader("Authorization", AUTH)?.build()
  60. chain?.proceed(request)
  61. }
  62. client.addInterceptor(interceptor) // Attaching the Interceptor
  63.  
  64.  
  65.  
  66. // Creating the instance of a Builder
  67. return Retrofit.Builder()
  68. .baseUrl("https://srodki.herokuapp.com/") // The API server
  69. .client(client.build()) // Adding Http Client
  70. .addConverterFactory(GsonConverterFactory.create()) // Object Converter
  71. .build()
  72. }
  73.  
  74. fun providesGetLogin(): GET_LOGIN = retrofit.create(GET_LOGIN::class.java)
  75. }
  76.  
  77. package com.joaomartins.srodkitrwale
  78.  
  79. import android.content.Intent
  80. import android.support.v7.app.AppCompatActivity
  81. import android.os.Bundle
  82. import android.text.Editable
  83. import android.util.Base64
  84. import android.util.Log
  85. import android.widget.Toast
  86. import kotlinx.android.synthetic.main.activity_login.*
  87. import retrofit2.Call
  88. import retrofit2.Callback
  89. import retrofit2.Response
  90. import retrofit2.Retrofit
  91.  
  92. class Login : AppCompatActivity() {
  93.  
  94.  
  95. val apiLogin = RetrofitClient().providesGetLogin().getAccessToken()
  96.  
  97. override fun onCreate(savedInstanceState: Bundle?) {
  98. super.onCreate(savedInstanceState)
  99. setContentView(R.layout.activity_login)
  100.  
  101.  
  102.  
  103. loginBtn.setOnClickListener {
  104. val user = userTxt.text
  105. val pass = passTxt.text
  106.  
  107.  
  108.  
  109. if (validateLogin(user, pass)){
  110. login(user, pass)
  111. }
  112. }
  113. }
  114.  
  115.  
  116.  
  117. fun validateLogin(user: Editable, pass: Editable): Boolean {
  118. if (user == null || user.trim().isEmpty()){
  119. Toast.makeText(this, "Missing Username or Password", Toast.LENGTH_SHORT).show()
  120. return false
  121. }
  122. if (pass == null || pass.trim().isEmpty()){
  123. Toast.makeText(this, "Missing Username or Password", Toast.LENGTH_SHORT).show()
  124. return false
  125. }
  126. return true
  127. }
  128.  
  129. fun login(user: Editable, pass: Editable) {
  130. /*val intent = Intent(this, List_users::class.java)
  131. startActivity(intent)*/
  132. apiLogin.enqueue(object : Callback<String> {
  133. override fun onResponse(call: Call<String>, response: Response<String>) {
  134. Log.d("check", "Reached this place")
  135. if(!response.isSuccessful)
  136. Log.d("failed", "No Success")
  137. Toast.makeText(this@Login, "Login Successful!", Toast.LENGTH_SHORT).show()
  138. val intent = Intent(this@Login, List_users::class.java)
  139. startActivity(intent)
  140. }
  141.  
  142. override fun onFailure(call: Call<String>, t: Throwable) {
  143. Toast.makeText(this@Login, "Login Failed.", Toast.LENGTH_SHORT).show()
  144. }
  145. })
  146.  
  147. }
  148.  
  149. }
  150.  
  151. val username = Login().userTxt.text
  152. val password = Login().passTxt.text
  153. val credentials = username + ":" + password
  154. val AUTH = "Basic " + Base64.encodeToString(credentials.toByteArray(Charsets.UTF_8), Base64.DEFAULT).replace("n", "")
Add Comment
Please, Sign In to add comment