改造传递字符串到URL

时间:2019-06-16 06:47:24

标签: android kotlin retrofit2

如何通过按一个按钮来传递apiInterface类的可变日期?

片段FragJornadas,我想从那里将​​变量传递给Interface中的@Get?

    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
    }
}

}

和ApiInterface类接收它。我想获取的网址是:

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

我要获取的网址是:

  

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

这是错误:

  

java.lang.IllegalArgumentException:URL查询字符串   “ action = getResultados&cmp = 328 {jor}&tmp = 2018/2019”不得具有   更换块。对于动态查询参数

     

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

2 个答案:

答案 0 :(得分:1)

使用serve.js告诉改型在URL路径中替换该变量,但您需要在查询字符串中。路径是域之后和查询字符串之前的位-在您的情况下将是@Path,也许是前面的位,如果不查看完整的网址,我就无法说完。

要向查询字符串中添加参数,您要使用server.php,并且不想在@Query批注的查询字符串中指定该参数。因此您可以将其更改为:

@GET

您已从查询字符串中删除@GET("server.php?action=getResultados&cmp=328&tmp=2018/2019") fun getTodasJuvenil( @Query("jor") jornada: String ): Call<List<TodasModel>>) ,并用{jor}替换了@Path

现在,改造应该在查询参数中添加所需的@Query

答案 1 :(得分:0)

@Fred感谢您的回答,我的错误不是上一个,我没有删除列表,而是在最后添加了,解决方法是:

dataList.clear()

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