When trying to sign in with some credentials on an android app an error message saying “error:” pops up, with no text or message and I’m not sure what the problem could be.
I am making this android app that will allow users to sign in with their company id and id fetched from a database and then do face recognition to prove it is them and then clock them in. Then they can clock out by doing the same face recognition process. The database being used is a MySQL run on a Laravel website hosted on ngrok to be used for testing purposes with the android app and and retrofit API for the android app to communicate with teh website and face++ API for the face recognition. It is supposed to sign in but with those credentials but it just displays that pop up saying “error:” and nothing more. The server is up and running and works so I am not sure what the problem is.
public function authenticate(Request $request)
{
$employee = User::where('Company_ID', $request->companyId)
->where('id', $request->employeeId)
->first();
if ($employee) {
return response()->json(['success' => true]);
} else {
return response()->json(['success' => false]);
}
}
package com.example.clockinclockout
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
class LoginActivity : AppCompatActivity() {
private lateinit var companyIdEditText: EditText
private lateinit var employeeIdEditText: EditText
private lateinit var clockInButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
companyIdEditText = findViewById(R.id.companyIdEditText)
employeeIdEditText = findViewById(R.id.employeeIdEditText)
clockInButton = findViewById(R.id.loginButton)
clockInButton.setOnClickListener {
val companyId = companyIdEditText.text.toString().toIntOrNull()
val employeeId = employeeIdEditText.text.toString().toIntOrNull()
if (companyId != null && employeeId != null) {
authenticateUser(companyId, employeeId)
} else {
Toast.makeText(this, "Please enter valid IDs", Toast.LENGTH_SHORT).show()
}
}
}
private fun authenticateUser(companyId: Int, employeeId: Int) {
val retrofit = Retrofit.Builder()
.baseUrl("https://5162-105-161-215-17.ngrok-free.app")
.addConverterFactory(GsonConverterFactory.create())
.build()
val api = retrofit.create(ApiService::class.java)
val authRequest = AuthRequest(companyId, employeeId)
api.authenticateUser(authRequest).enqueue(object : Callback<AuthResponse> {
override fun onResponse(call: Call<AuthResponse>, response: Response<AuthResponse>) {
if (response.isSuccessful) {
val authResponse = response.body()
if (authResponse != null && authResponse.success) {
val intent = Intent(this@LoginActivity, FaceRecognitionActivity::class.java)
intent.putExtra("companyId", companyId)
intent.putExtra("employeeId", employeeId)
startActivity(intent)
finish()
} else {
Toast.makeText(this@LoginActivity, "Invalid credentials", Toast.LENGTH_SHORT).show()
}
} else {
Toast.makeText(this@LoginActivity, "Error: ${response.message()}", Toast.LENGTH_SHORT).show()
}
}
override fun onFailure(call: Call<AuthResponse>, t: Throwable) {
Toast.makeText(this@LoginActivity, "Network error: ${t.message}", Toast.LENGTH_SHORT).show()
}
})
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">
<EditText
android:id="@+id/companyIdEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Company ID"
android:inputType="number" />
<EditText
android:id="@+id/employeeIdEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Employee ID"
android:inputType="number"
android:layout_marginTop="16dp"/>
<Button
android:id="@+id/loginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clock In"
android:layout_marginTop="16dp"/>
</LinearLayout>
package com.example.clockinclockout
import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.POST
interface ApiService {
@POST("verify-face")
fun verifyFace(@Body request: FaceRequest): Call<VerifyFaceResponse>
@POST("save-face")
fun saveFace(@Body request: FaceRequest): Call<SaveFaceResponse>
@POST("clock-in")
fun clockIn(@Body request: ClockInRequest): Call<ClockInResponse>
@POST("clock-out")
fun clockOut(@Body request: ClockOutRequest): Call<ClockOutResponse>
@POST("authenticate")
fun authenticateUser(@Body request: AuthRequest): Call<AuthResponse>
}
data class FaceRequest(val companyId: Int, val employeeId: Int, val image: ByteArray)
data class VerifyFaceResponse(val success: Boolean)
data class SaveFaceResponse(val success: Boolean)
data class ClockInRequest(val companyId: Int, val employeeId: Int, val image: ByteArray)
data class ClockInResponse(val success: Boolean)
data class ClockOutRequest(val companyId: Int, val employeeId: Int, val image: ByteArray)
data class ClockOutResponse(val success: Boolean)
data class AuthRequest(val companyId: Int, val employeeId: Int)
data class AuthResponse(val success: Boolean)
This is the code for the clock in functionality in Kotlin and it’s corresponding XML file as well as the PHP controller file where the android app is posting the request.