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);
}
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())
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.