科特林 |改造 API 接电话 access_token

Kotlin | Retrofit API Get call with access_token

我正在尝试在 Kotlin (Android 9+) 中使用 Retrofit 构建 API Get 请求,但在使用通过 Intent 获得的 apikey 时卡住了。下面是代码片段。

class Alerts25Activity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_alerts25)
        getAlert25Data()
    }

    private fun getAlert25Data() {
        val cname:String = intent.getStringExtra("cname").toString()
        val apikey:String = intent.getStringExtra("apikey").toString()
        val instanceurl = "https://" + cname + ".something.com/api/"
        val retrofitBuilder = Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(instanceurl)
            .build()
            .create(Alerts25Interface::class.java)
        val retrofitData = retrofitBuilder.getData()

        retrofitData.enqueue(object : Callback<List<Alerts25DataClass>?> {
            @SuppressLint("NotifyDataSetChanged")
            override fun onResponse(
                call: Call<List<Alerts25DataClass>?>,
                response: Response<List<Alerts25DataClass>?>
            ) {
                val responseBody = response.body()!!
                lateinit var alerts25Adapter: Alerts25Adapter
                lateinit var linearLayoutManager: LinearLayoutManager

                linearLayoutManager = LinearLayoutManager(this@Alerts25Activity)
                val recyclerViewUsers = findViewById<RecyclerView>(R.id.recyclerViewUsers)
                recyclerViewUsers.layoutManager = linearLayoutManager

                alerts25Adapter = Alerts25Adapter(baseContext, responseBody)
                alerts25Adapter.notifyDataSetChanged()
                recyclerViewUsers.adapter = alerts25Adapter

            }

            override fun onFailure(call: Call<List<Alerts25DataClass>?>, t: Throwable) {
                Log.d("MainActivity", "Message: " + t.message)
            }
        })
    }
}

interface Alerts25Interface {
    @GET("Alerts?access_token=xxxxxxxxxxxxxxxxxxxxxxxx")
    fun getData(): Call<List<Alerts25DataClass>>
}

class Alerts25Adapter (val context: Context, val Alert25List: List<Alerts25DataClass>): RecyclerView.Adapter<Alerts25Adapter.ViewHolder>() {

    class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
        val name: TextView
        val type: TextView
        val sourceType: TextView
        val createdOn: TextView

        init {
            name = itemView.findViewById(R.id.name)
            type = itemView.findViewById(R.id.type)
            sourceType = itemView.findViewById(R.id.sourceType)
            createdOn = itemView.findViewById(R.id.createdOn)
        }

    }

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.name.text = Alert25List[position].name
        holder.type.text = Alert25List[position].type
        holder.sourceType.text = Alert25List[position].sourceType
        holder.createdOn.text = Alert25List[position].createdOn.replace("T", " ").split(".")[0]
    }

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


data class Alerts25DataClass(
    val name: String,
    val createdOn: String,
    val sourceType: String,
    val type: String
)

如果我在@GET 中对 apikey 进行硬编码(如上例所示),它会按预期工作。但我想使用通过 intent 方法检索的 apikey(Intent 来自 main activity)

interface Alerts25Interface {
    @GET("Alerts")
    fun getData(@Query("access_token") apiKey: String): Call<List<Alerts25DataClass>>
}

然后调用:

val retrofitData = retrofitBuilder.getData(apikey)