diff --git a/src/apps/Android/app/src/main/AndroidManifest.xml b/src/apps/Android/app/src/main/AndroidManifest.xml index 14e8f78..7c61be7 100644 --- a/src/apps/Android/app/src/main/AndroidManifest.xml +++ b/src/apps/Android/app/src/main/AndroidManifest.xml @@ -21,6 +21,10 @@ + + diff --git a/src/apps/Android/app/src/main/java/com/janishutz/libreevent/ApiClient.kt b/src/apps/Android/app/src/main/java/com/janishutz/libreevent/ApiClient.kt new file mode 100644 index 0000000..edb9b91 --- /dev/null +++ b/src/apps/Android/app/src/main/java/com/janishutz/libreevent/ApiClient.kt @@ -0,0 +1,108 @@ +package com.janishutz.libreevent + +import java.io.BufferedReader +import java.io.DataOutputStream +import java.io.InputStreamReader +import java.lang.Exception +import java.net.HttpURLConnection +import java.net.URL + +class ApiClient { + fun authenticateUser(apiUrl: String, username: String, password: String): String { + try { + val url = URL("$apiUrl/app/authenticate") + println(url) + val connection = url.openConnection() as HttpURLConnection + + // Set the request method to POST + connection.requestMethod = "POST" + + // 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 = "{\"username\":\"$username\",\"password\":\"$password\"}" + + // Write the JSON data to the output stream + val outputStream = DataOutputStream(connection.outputStream) + outputStream.write(jsonRequest.toByteArray(Charsets.UTF_8)) + outputStream.flush() + outputStream.close() + + // Get the response code from the server + val responseCode = connection.responseCode + + if (responseCode == HttpURLConnection.HTTP_OK) { + // Read and handle the response from the server + val reader = BufferedReader(InputStreamReader(connection.inputStream)) + val response = StringBuilder() + var line: String? + while (reader.readLine().also { line = it } != null) { + response.append(line) + } + reader.close() + + // 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 "status-code-non-ok" + } + } catch (e: Exception) { + e.printStackTrace() + return "error" + } + } + + fun checkTicket(apiUrl: String, username: String, password: String, ticket: String): String { + val url = URL("$apiUrl/app/ticketLookup") + val connection = url.openConnection() as HttpURLConnection + + // Set the request method to POST + connection.requestMethod = "POST" + + // 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 = "{\"username\":\"$username\",\"password\":\"$password\",\"ticketID\":$ticket}" + + // Write the JSON data to the output stream + val outputStream = DataOutputStream(connection.outputStream) + outputStream.write(jsonRequest.toByteArray(Charsets.UTF_8)) + outputStream.flush() + outputStream.close() + + // Get the response code from the server + val responseCode = connection.responseCode + + if (responseCode == HttpURLConnection.HTTP_OK) { + // Read and handle the response from the server + val reader = BufferedReader(InputStreamReader(connection.inputStream)) + val response = StringBuilder() + var line: String? + while (reader.readLine().also { line = it } != null) { + response.append(line) + } + reader.close() + + // 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 "" + } + } +} \ No newline at end of file diff --git a/src/apps/Android/app/src/main/java/com/janishutz/libreevent/MainActivity.kt b/src/apps/Android/app/src/main/java/com/janishutz/libreevent/MainActivity.kt index 2639cd4..eb8d6ed 100644 --- a/src/apps/Android/app/src/main/java/com/janishutz/libreevent/MainActivity.kt +++ b/src/apps/Android/app/src/main/java/com/janishutz/libreevent/MainActivity.kt @@ -4,6 +4,9 @@ import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Button +import android.widget.EditText +import android.app.AlertDialog +import com.janishutz.libreevent.ApiClient class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { @@ -11,13 +14,54 @@ class MainActivity : AppCompatActivity() { setContentView(R.layout.activity_main) val loginButton = findViewById