Home > OS >  how to fetch respose data by retrofit with multiple parameters By GET Request in Jetpack compose
how to fetch respose data by retrofit with multiple parameters By GET Request in Jetpack compose

Time:10-14

In these days i am building An english learning app for beginners so i have problem, i want to fetch response data with two parameters (level and unit) from database as shown in php code by retrofit Like this,

https://adeega.xisaabso.online/Api/Article_vocabulary.php?Level=1&Unit=1

so how can i do it? I need help to do that? Here is my Code Below

Vocabulary_Api.kt

const val Vocabulary = "https://adeega.xisaabso.online/Api/"

interface getVocabularies {
   @GET("Article_vocabulary.php")
   fun getVocabularies(): Call<Vocabularies>
}

object getVocabulariesInstance {
   val getVocabulariesSections: getVocabularies

   init {
      val retrofit = Retrofit.Builder()
         .baseUrl(Vocabulary)
         .addConverterFactory(GsonConverterFactory.create())
         .build()
      getVocabulariesSections = retrofit.create(getVocabularies::class.java)
   }
}

Vocabulary.kt

  val users = getVocabulariesInstance.getVocabulariesSections.getVocabularies()

  val data = remember { mutableStateOf(Vocabularies()) }
  users.enqueue(object: Callback<Vocabularies> {
     override fun onResponse(
        call: Call<Vocabularies>,
        response: Response<Vocabularies>
     ) {
        val userData = response.body()
        if(userData != null) {
           data.value = userData
        }
     }
     override fun onFailure(call: Call<Vocabularies>, t: Throwable) {
        Toast.makeText(context, t.toString(), Toast.LENGTH_SHORT).show()
     }
  })


  LazyColumn(
     contentPadding = PaddingValues(
        horizontal = 12.dp,
        /*vertical = 0.dp*/
     ),
     verticalArrangement = Arrangement.spacedBy(10.dp)
  ) {
     items(data.value) { item ->
        Cards_Vocabulary(data = item, navController)
        //Divider(color = Color.Gray, thickness = 1.dp)
     }
  } // END LazyColumn

database.php

if(!empty($_GET['Level']) && !empty($_GET['Unit'])) {

    $Level = (int)$_GET['Level'];
    $Unit  = (int)$_GET['Unit'];


    $Article = $pdo->prepare("SELECT * FROM App_vocabularies 
                                WHERE LevelID = ? AND UnitID = ?");
    $Article->bindParam(1, $Level);
    $Article->bindParam(2, $Unit);
    $Article->execute();
    $Article = $Article->fetchAll(PDO::FETCH_OBJ);
        echo json_encode($Article);

}

CodePudding user response:

If you want to know how to pass parameters as queryString to a query, here is what to do

interface getVocabularies {
    @GET("Article_vocabulary.php")
    fun getVocabularies(@Query("Level") level: Int, @Query("Unit") unit: Int): Call<Vocabularies>
}

Vocabulary.kt

getVocabulariesInstance.getVocabulariesSections.getVocabularies(aLevel, anUnit)

Or if you want to avoid multiple function parameters and pass all at once

interface getVocabularies {
    @GET("Article_vocabulary.php")
    fun getVocabularies(@QueryMap queries: Map<String, String>): Call<Vocabularies>
}

Vocabulary.kt

val params : MutableMap<String, String> = mutableMapOf()
params["Level"] = aLevel
params["Unit"] = anUnit

getVocabulariesInstance.getVocabulariesSections.getVocabularies(params.toImmutableMap())
  • Related