Search code examples
androidkotlinretrofit2

callback always fails with retrofit url


this is the mainactivity

val url:String="http://api.brainshop.ai/get?bid=167083&key=jIpZgrpRgALYSZ72&uid=[uid]&msg="+message
            val BASE_URL="http://api.brainshop.ai/"
            val retrofit=Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
            val retrofitAPI=retrofit.create(RetrofitAPI::class.java)
            val call:retrofit2.Call<MsgModal> = retrofitAPI.getMessage(url)

            call.enqueue(object: Callback<MsgModal>{
                override fun onResponse(call: retrofit2.Call<MsgModal>, response: Response<MsgModal>) {
                    val modal: MsgModal = response.body()!!
                    Toast.makeText(applicationContext,"bsh", Toast.LENGTH_SHORT).show()
                    messageModalArrayList.plusAssign(ChatsModal(modal.cnt, BOT_KEY))
                    Toast.makeText(applicationContext, modal.cnt, Toast.LENGTH_SHORT).show()
                    chatRVAdapter.notifyDataSetChanged()
                }
                override fun onFailure(call: retrofit2.Call<MsgModal>, t: Throwable) {
                    messageModalArrayList.add(ChatsModal("please revert your question",BOT_KEY))
                    chatRVAdapter.notifyDataSetChanged()
                }

            })

Msgmodal.kt

class MsgModal (
    var cnt: String
        )

retroapi interface

interface RetrofitAPI {
    @GET
    fun getMessage(@Url url: String ): Call<MsgModal>
}

there is no error in the logcat...and the bot message is always directed to onfailure part in callback


Solution

  • You can fix that by simply adding

    android:usesCleartextTraffic="true"
    

    To your <application> tag in the manifest