Kotlin Retrofit cannot return data made in onRespons function

301 views Asked by At

I am trying to connect to my database and find username and passwords. I am using Kotlin with retrofit and cannot return data made up in onResponse function.

I am trying get datas like logName, logPassword, logId and isUser outside of this function. This var are global.

private fun getCredentials(username: String, password: String): Int {
        val retrofitBuilder = Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(BASE_URL)
            .build()
            .create(Api::class.java)
        val retrofitData = retrofitBuilder.getUsers()

            retrofitData.enqueue(object : Callback<UserResults> {
                override fun onResponse(
                    call: Call<UserResults>,
                    response: Response<UserResults>
                ) {
                    val responseBody = response.body()!!

                    val myStringBuilder = StringBuilder()

                    for (myData in responseBody.results.listIterator()) {
                        myStringBuilder.append(myData.name)

                        if (myData.name.equals(username) && myData.password.equals(password)) {
                            Log.d(
                                "-------------------",
                                "Database:" + myData.name + " Input:" + username
                            )

                            logName = myData.name
                            logPassword = myData.password
                            logId = myData.id
                            isUser = 1


                            Log.d("-------------------", "isUser? in database loop:" + isUser)
                        }
                    }

                }
                override fun onFailure(call: Call<UserResults>, t: Throwable) {
                    Log.d("ERROR With BackEnd", "Error: " + t.message)
                }
            })

        Log.d("-------------", "isUser end of getCredentials: " + isUser)
        Log.d("-------------------", "Database:" +logName+ " Input:"+username )
        return isUser
    }
0

There are 0 answers