Changed to read header

This commit is contained in:
wiecktobi 2020-05-11 20:59:32 +02:00
parent e6834a18e6
commit b4d6b5dd0c
2 changed files with 18 additions and 11 deletions

View File

@ -1,14 +1,13 @@
package de.hft.geotracker package de.hft.geotracker
import okhttp3.Authenticator
import retrofit2.Call import retrofit2.Call
import retrofit2.http.GET import retrofit2.http.*
import retrofit2.http.Path
import retrofit2.http.Query
interface GeofenceService { interface GeofenceService {
@GET("/login?username=user&password=pw") @POST("/login")
fun login(@Query("user") user : String, fun login(@Query("user") user : String,
@Query("pw") password : String) : Call<String> @Query("pw") password : String) : Call<Headers>

View File

@ -9,12 +9,15 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import okhttp3.Authenticator
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import retrofit2.Call import retrofit2.Call
import retrofit2.Callback import retrofit2.Callback
import retrofit2.Response import retrofit2.Response
import retrofit2.Retrofit import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Header
import retrofit2.http.Headers
import java.net.HttpRetryException import java.net.HttpRetryException
/** /**
@ -51,21 +54,26 @@ class Login : AppCompatActivity() {
} }
private fun login() { private fun login() {
var call : Call<String> = service.login("wito","tobias") var call= service.login("wito","tobias")
call.enqueue(object : Callback<String> {
override fun onResponse(call: Call<String>?, response: Response<String>?) { call.enqueue(object : Callback<Headers> {
override fun onResponse(call: Call<Headers>?, response: Response<Headers>?) {
if(response != null && response.isSuccessful) { if(response != null && response.isSuccessful) {
response.body()
token = JWToken(call.toString()) var headers = response.headers()
var authentication = headers.get("Authorization")
token = JWToken(authentication)
println("Erfolg") println("Erfolg")
} else { } else {
println("Fehler1") println("Fehler1")
} }
} }
override fun onFailure(call: Call<String>?, t: Throwable?) { override fun onFailure(call: Call<Headers>?, t: Throwable?) {
println("Fehler2 ${t.toString()}") println("Fehler2 ${t.toString()}")
} }
}) })
// var token = call.execute().body() // var token = call.execute().body()
// val intent = Intent(this, MainActivity::class.java) // val intent = Intent(this, MainActivity::class.java)