almost finished android app

This commit is contained in:
2023-10-09 19:17:19 +02:00
parent 45562414e4
commit 839e118129
3 changed files with 39 additions and 14 deletions

View File

@@ -19,7 +19,6 @@ class ApiClient {
// Set request headers (if needed)
connection.setRequestProperty("Content-Type", "application/json")
// Add other headers as needed
// Enable input and output streams for the connection
connection.doInput = true
@@ -60,7 +59,11 @@ class ApiClient {
}
fun checkTicket(apiUrl: String, username: String, password: String, ticket: String): String {
val url = URL("https://$apiUrl/app/ticketLookup")
var url = URL("$apiUrl/app/ticketLookup")
if ( !apiUrl.contains( "https://" )) {
url = URL("https://$apiUrl/app/ticketLookup")
}
val connection = url.openConnection() as HttpURLConnection
// Set the request method to POST
@@ -68,14 +71,13 @@ class ApiClient {
// Set request headers (if needed)
connection.setRequestProperty("Content-Type", "application/json")
// Add other headers as needed
// Enable input and output streams for the connection
connection.doInput = true
connection.doOutput = true
// Create the JSON request body
val jsonRequest = "{\"email\":\"$username\",\"password\":\"$password\",\"ticketID\":$ticket}"
val jsonRequest = "{\"email\":\"$username\",\"password\":\"$password\",\"ticketID\":\"$ticket\"}"
// Write the JSON data to the output stream
val outputStream = DataOutputStream(connection.outputStream)
@@ -99,9 +101,16 @@ class ApiClient {
// Return the response as a String
return response.toString()
} else {
// Handle the error (e.g., authentication failed)
// You can also throw an exception here if needed
return ""
val r = BufferedReader(InputStreamReader(connection.errorStream))
val res = StringBuilder()
var line: String?
while (r.readLine().also { line = it } != null) {
res.append(line)
}
r.close()
println(res.toString())
return "Error"
}
}
}

View File

@@ -18,7 +18,7 @@ class MainActivity : AppCompatActivity() {
val policy = ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
val sharedPref = getSharedPreferences( "login", MODE_PRIVATE )
val sharedPref = applicationContext.getSharedPreferences( "login", MODE_PRIVATE )
val hasSwitched = intent.hasExtra("hasSwitched")
@@ -50,7 +50,7 @@ class MainActivity : AppCompatActivity() {
val res = ApiClient().authenticateUser( url, username, password )
println( res )
if ( res == "authOk" ) {
val sharedPref = getSharedPreferences( "login", MODE_PRIVATE )
val sharedPref = applicationContext.getSharedPreferences( "login", MODE_PRIVATE )
val editor = sharedPref.edit()
editor.putString( "username", username )
editor.putString( "password", password )

View File

@@ -27,11 +27,10 @@ class ScannerActivity : CaptureActivity() {
val logoutButton = findViewById<Button>(R.id.logoutButton)
logoutButton.setOnClickListener {
val sharedPref = getSharedPreferences( "login", MODE_PRIVATE )
val sharedPref = applicationContext.getSharedPreferences( "login", MODE_PRIVATE )
val editor = sharedPref.edit()
editor.remove( "password" )
editor.remove( "loginOk" )
editor.remove( "username" )
editor.remove( "url" )
editor.apply()
val switchIntent = Intent(this, MainActivity::class.java)
switchIntent.putExtra("hasSwitched", true)
@@ -63,12 +62,29 @@ class ScannerActivity : CaptureActivity() {
private fun handleScanResult(result: String) {
if ( lastScanned != result ) {
val sharedPref = getSharedPreferences( "login", MODE_PRIVATE )
val sharedPref = applicationContext.getSharedPreferences( "login", MODE_PRIVATE )
ApiClient().checkTicket( sharedPref.getString( "url", null ).toString(),
val status = ApiClient().checkTicket( sharedPref.getString( "url", null ).toString(),
sharedPref.getString( "username", null ).toString(),
sharedPref.getString( "password", null ).toString(), result )
lastScanned = result
val alertDialogBuilder = AlertDialog.Builder(this)
if ( status == "ticketValid" ) {
alertDialogBuilder.setTitle("Ticket is valid")
} else if ( status == "ticketInvalid" ) {
alertDialogBuilder.setTitle("Ticket is invalid")
} else if ( status == "Error" ) {
alertDialogBuilder.setTitle("There was an error connecting")
alertDialogBuilder.setMessage("Please log out and log in again")
}
alertDialogBuilder.setIcon(android.R.drawable.ic_dialog_alert)
alertDialogBuilder.setPositiveButton("OK") { dialog, _ ->
dialog.dismiss()
}
alertDialogBuilder.show()
}
}