I am trying to build API Get request using Retrofit in Kotlin (Android 9 ) and got stuck using apikey which is obtained via intent. Below is the code snip.
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
)
If I hardcode the apikey in @GET (As shown in above example), it works as expected. But I want to use the apikey which is retrieved via intent method (Intent coming from main activity)
CodePudding user response:
interface Alerts25Interface {
@GET("Alerts")
fun getData(@Query("access_token") apiKey: String): Call<List<Alerts25DataClass>>
}
And then call :
val retrofitData = retrofitBuilder.getData(apikey)