Created Login with Retrofit and store Token (with exceptions)

This commit is contained in:
wiecktobi 2020-05-11 14:24:01 +02:00
parent d66ecc43b7
commit e6834a18e6
6 changed files with 62 additions and 2 deletions

View File

@ -45,4 +45,7 @@ dependencies {
implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'
implementation "android.arch.navigation:navigation-fragment-ktx:2.2.2"
implementation "android.arch.navigation:navigation-ui-ktx:2.2.2"
implementation 'com.squareup.retrofit2:retrofit:2.1.0'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
}

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.hft.geotracker">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
@ -9,6 +10,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Register"></activity>
<activity
android:name=".Settings"

View File

@ -0,0 +1,15 @@
package de.hft.geotracker
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query
interface GeofenceService {
@GET("/login?username=user&password=pw")
fun login(@Query("user") user : String,
@Query("pw") password : String) : Call<String>
}

View File

@ -0,0 +1,3 @@
package de.hft.geotracker
data class JWToken (var token : String)

View File

@ -5,6 +5,17 @@ import android.os.Bundle
import androidx.fragment.app.Fragment
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.net.HttpRetryException
/**
* A simple [Fragment] subclass.
@ -12,9 +23,18 @@ import androidx.appcompat.app.AppCompatActivity
class Login : AppCompatActivity() {
lateinit var login : TextView
lateinit var reg : TextView
lateinit var service : GeofenceService
lateinit var token : JWToken
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
val retrofit = Retrofit.Builder()
.baseUrl("http://localhost:5000")
.addConverterFactory(GsonConverterFactory.create())
.build()
service = retrofit.create(GeofenceService::class.java)
login = findViewById(R.id.button_create_account)
login.setOnClickListener {
login()
@ -31,7 +51,24 @@ class Login : AppCompatActivity() {
}
private fun login() {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
var call : Call<String> = service.login("wito","tobias")
call.enqueue(object : Callback<String> {
override fun onResponse(call: Call<String>?, response: Response<String>?) {
if(response != null && response.isSuccessful) {
response.body()
token = JWToken(call.toString())
println("Erfolg")
} else {
println("Fehler1")
}
}
override fun onFailure(call: Call<String>?, t: Throwable?) {
println("Fehler2 ${t.toString()}")
}
})
// var token = call.execute().body()
// val intent = Intent(this, MainActivity::class.java)
// startActivity(intent)
}
}