Rafel C.F
Rafel C.F

Reputation: 179

Retrofit pass String to url

How can I pass the variable day by pressing a button to the apiInterface class?

The fragment FragJornadas, from where I want to pass the variable to @Get in Interface?

    class FragJornadas : Fragment() {

var jornada =  "1"
var dataList = ArrayList<TodasModel>()
lateinit var recyclerView: RecyclerView

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?): View? {
    // Inflate the layout for this fragment
    val view = inflater.inflate(R.layout.jornadas_list, container, false)

    val miTexto: TextView = view.findViewById(R.id.tv_Jornadas)
    miTexto.text = (getString(R.string.num_jornada))

    val numJor = intArrayOf(R.id.tv_01, R.id.tv_02, R.id.tv_03, R.id.tv_04, R.id.tv_05,
        R.id.tv_06, R.id.tv_07, R.id.tv_08, R.id.tv_09, R.id.tv_10, R.id.tv_11, R.id.tv_12,
        R.id.tv_13, R.id.tv_14, R.id.tv_15, R.id.tv_16, R.id.tv_17, R.id.tv_18, R.id.tv_19,
        R.id.tv_20, R.id.tv_21, R.id.tv_22, R.id.tv_23, R.id.tv_24, R.id.tv_25, R.id.tv_26,
        R.id.tv_27, R.id.tv_28, R.id.tv_29, R.id.tv_30)

    val button = arrayOfNulls<Button>(numJor.size)

    for(i in numJor.indices){
        button[i] = view.findViewById(numJor[i]) as Button
        val buttonValue = i+1
        val buttonText = Integer.toString(buttonValue)

        button[i]!!.setOnClickListener {
            miTexto.text = getString(R.string.num_jornada) + " " + buttonText
            jornada = buttonText

            getData(jornada)
        }
    }

    return view
}


override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val progressBar: ProgressBar = this.progressBar1

    recyclerView = view.findViewById(R.id.recycler_view)
    recyclerView.adapter= TodasAdapter(dataList,activity!!)
    recyclerView.layoutManager=LinearLayoutManager(activity!!,LinearLayoutManager.VERTICAL,false)

    Thread(Runnable {
        activity!!.runOnUiThread {
            progressBar.visibility = View.VISIBLE
        }

        try {
            var i = 0
            while(i < Int.MAX_VALUE){
                i++
            }
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }

        activity!!.runOnUiThread {
            progressBar.visibility = View.GONE
        }
    }).start()
    getData(jornada)
}

private fun getData(jornada: String) {
    val call: Call<List<TodasModel>> = ApiFederacion.getClient.getTodasJuvenil(jornada)
    call.enqueue(object : Callback<List<TodasModel>> {

        override fun onResponse(call: Call<List<TodasModel>>?, response: Response<List<TodasModel>>?) {
            dataList.addAll(response!!.body()!!)
            recyclerView.adapter!!.notifyDataSetChanged()
        }

        override fun onFailure(call: Call<List<TodasModel>>?, t: Throwable?) {
            //progerssProgressDialog.dismiss()
        }
    })
}

TodasAdapter

class TodasAdapter(private var actList: List<TodasModel>, private val context: Context) : RecyclerView.Adapter<TodasAdapter.ViewHolder>() {


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    return ViewHolder(LayoutInflater.from(context).inflate(R.layout.actual_row, parent, false))
}

override fun getItemCount(): Int {
   return actList.size
}

@SuppressLint("SetTextI18n", "SimpleDateFormat")
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val dataModel= actList.get(position)

    holder.itemView.setOnClickListener(){

        Toast.makeText(context, dataModel.nomLocal, Toast.LENGTH_LONG).show()
    }

    val parser = SimpleDateFormat("dd/MM/yyyy") //Formato de fecha obtenida
    val formatter = SimpleDateFormat("EE dd 'de' MMMM 'del 'yyyy \n" + "'  a las '") //Formato a mostrar
    val output = formatter.format(parser.parse(dataModel.fecha + "/2019"))

    holder.tv_Fecha.text = output + dataModel.hora
    //holder.tv_Fecha.text = dataModel.fecha + "\n" + dataModel.hora
    holder.tv_NomLocal.text = dataModel.nomLocal
    holder.tv_ResultadoL.text = dataModel.resulLocal
    holder.tv_Estado.text = " - " + dataModel.estadoPartido + " - "
    holder.tv_ResultadoV.text = dataModel.resulVisitante
    holder.tv_NomVisitante.text = dataModel.nomVisitante

    Picasso.get()
        .load("https://ffcv.es/ncompeticiones/" + dataModel.escudoLocal)
        .fit()
        //.resize(150, 150)
        .into(holder.imageEscLocal)

    Picasso.get()
        .load("https://ffcv.es/ncompeticiones/" + dataModel.escudoVisitante)
        .fit()
        .into(holder.imageEscVisi)
}


class ViewHolder(itemLayoutView: View) : RecyclerView.ViewHolder(itemLayoutView) {
    var tv_Fecha:TextView
    var tv_NomLocal:TextView
    var tv_ResultadoL:TextView
    var tv_Estado:TextView
    var tv_ResultadoV:TextView
    var tv_NomVisitante:TextView
    var imageEscLocal: ImageView
    var imageEscVisi: ImageView

    init {
        tv_Fecha = itemLayoutView.findViewById(R.id.tv_Fecha)
        tv_NomLocal = itemLayoutView.findViewById(R.id.tv_Equipo_Local)
        tv_ResultadoL = itemLayoutView.findViewById(R.id.tv_Result_Local)
        tv_Estado = itemLayoutView.findViewById(R.id.tv_Estado)
        tv_ResultadoV = itemLayoutView.findViewById(R.id.tv_Result_Visitante)
        tv_NomVisitante = itemLayoutView.findViewById(R.id.tv_Equipo_Visitante)

        imageEscLocal = itemLayoutView.findViewById(R.id.iv_Local) as ImageView
        imageEscVisi = itemLayoutView.findViewById(R.id.iv_Visi) as ImageView
    }
}

}

And the ApiInterface class to receive it. Being the url I want to get is:

@GET("server.php?action=getResultados&cmp=328{jor}tmp=2018/2019") fun
getTodasJuvenil(
    @Path("jor") jornada: String ): Call<List<TodasModel>>

The url I want to get is:

"server.php?action=getResultados&cmp=328&jor=1&tmp=2018/2019"

this is the error:

java.lang.IllegalArgumentException: URL query string "action=getResultados&cmp=328{jor}&tmp=2018/2019" must not have replace block. For dynamic query parameters

at com.myapplication.Jornadas.FragJornadas.getData(FragJornadas.kt:91) at com.myapplication.Jornadas.FragJornadas.onViewCreated(FragJornadas.kt:87)

Upvotes: 0

Views: 119

Answers (2)

Rafel C.F
Rafel C.F

Reputation: 179

@Fred thanks for your answers, my mistake was not the previous one, I was not deleting the list, it was added at the end, the solution is:

dataList.clear()

override fun onResponse(call: Call<List<TodasModel>>?, response: Response<List<TodasModel>>?) {
                dataList.clear()
                dataList.addAll(response!!.body()!!)
                recyclerView.adapter!!.notifyDataSetChanged()
            }

Upvotes: 0

Fred
Fred

Reputation: 17085

Using @Path you're telling retrofit to replace that variable in the path of the url, but you want in the query string. The path is the bit after the domain and before the query string - in your case would be server.php and maybe some bits that come before, which I can't say without seeing the full url.

To add a parameter to the query string you want to use @Query and you don't want to specify it in the query string in the @GET annotation. So you could change things to:

@GET("server.php?action=getResultados&cmp=328&tmp=2018/2019") fun
getTodasJuvenil(
  @Query("jor") jornada: String ): Call<List<TodasModel>>)

You've removed {jor} from the query string and replaced @Path with @Query.

Now, retrofit should add a query parameter with the desired jornada.

Upvotes: 1

Related Questions