0

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
    }
Alex Poliak
  • 33
  • 1
  • 4
  • Does this answer your question? [Why does my function that calls an API return an empty or null value?](https://stackoverflow.com/questions/57330766/why-does-my-function-that-calls-an-api-return-an-empty-or-null-value) – a_local_nobody Apr 19 '22 at 11:29
  • `cannot return data made up in onResponse function.` yes, that's an async function which responds at a later point in time, make use of callbacks, coroutines, live data, or any combination of these to achieve what you're looking for – a_local_nobody Apr 19 '22 at 11:30
  • I am trying everything, also I am kinda new at this. Isnt there some simple way to do it? – Alex Poliak Apr 19 '22 at 12:40
  • `Isnt there some simple way to do it?` unfortunately not, by nature that's not how async code works, the post i've linked should explain all of that to you, my advice would be to try make use of callbacks – a_local_nobody Apr 19 '22 at 14:47

0 Answers0