Search code examples
androidkotlinretrofitstrapi

Retrofit and GET using parameter


I want to create a service API with a GET method for filtering data. I created this API using Strapi.io. The Strapi documentation requires using $containsi to perform data filters. I want to replace the forest value with a dynamic value inputted via editText. How do I do that?

This is my ApiService

@GET("destinations?populate=*&filters[name][\$containsi]={name}")
fun getSearchList(@Path("name") name : String) : Call<ResponseDestination>

And this is code for my activity.

val et_text = view.findViewById<EditText>(R.id.et_text)
        val text = et_text.text.toString()

        // I want to replace the text from textView with "Forest" on getSearchList
        ApiConfigDestionation.getService().getSearchList("Forest").enqueue(object : Callback<ResponseDestination>{
            @SuppressLint("NotifyDataSetChanged")
            override fun onResponse(call: Call<ResponseDestination>, response: Response<ResponseDestination>) {
                val responseBody = response.body()
                val responseList = responseBody?.data
                val searhAdapter = ListAllAdapter(responseList)
                rv_destination.apply {
                    layoutManager = LinearLayoutManager(view?.context)
                    setHasFixedSize(true)
                    searhAdapter.notifyDataSetChanged()
                    adapter = searhAdapter
                }
            }

            override fun onFailure(call: Call<ResponseDestination>, t: Throwable) {
                Toast.makeText(view?.context, t.localizedMessage, Toast.LENGTH_SHORT).show()
            }
        })

        return view

I want to replace the text from textView with "name" on getSearchList.

Strapi Docs


Solution

  • I have resolved this issue. Simply alter ApiService. 

    From :

    @GET("destinations?populate=*&filters[name][\$containsi]={name}")
    fun getSearchList(@Path("name") name : String) : Call<ResponseDestination>
    

    To :

    @GET("destinations?populate=*&")
    fun getSearchList(@Query("filters[name][\$containsi]=name") name : String) : Call<ResponseDestination>
    

    Maybe someone else is experiencing the same thing; if so, please try this method.