How to Edit a Query Based on User Input

Viewed 25

Is there a way to edit a query for an API based on what a user enters into an EditText? I'm trying to have someone enter their username, then add that username string to my API call to get the Json response, but I can't find a way to do that without getting an error for changing my query string.

The code for my API Service (with a hardcoded QUERY_STRING):

private const val QUERY_STRING = "players/%232LU0RYYG"

private const val BASE_URL = "https://api.clashroyale.com/v1/"

private const val API_KEY = ""

private val client = OkHttpClient.Builder().addInterceptor{chain ->
    val newRequest: Request = chain.request().newBuilder()
        .addHeader("Authorization", "Bearer " + API_KEY)
        .build()
    chain.proceed(newRequest)
}.build()

private val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()

private val retrofit = Retrofit.Builder().client(client).addConverterFactory(MoshiConverterFactory.create(moshi)).baseUrl(BASE_URL).build()

interface ClashAPIService {
    @GET(QUERY_STRING)
    fun getStats(): Call<ClashResponse>
}

object StatAPI{
    val statApi: ClashAPIService by lazy {
        retrofit.create(ClashAPIService::class.java)
    }
}

The code for the class that I'm getting the username from:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentSubmitPlayerInfoBinding.inflate(inflater, container, false)
        val playerID : EditText = binding.enterPlayerID
        playerID.setText("%23")
        val playerUsername = ""
        binding.submitButton.setOnClickListener{
            playerUsername = playerID.text.toString()
        }
        return binding.root
    }

If there's any more code that I should supply that would help answer my question, just let me know. Thanks!

0 Answers
Related