feat : ajout recup des questions pour QuizMultiActivity
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
5db24cad2f
commit
8e11a953d4
@ -0,0 +1,63 @@
|
||||
package com.example.mathseduc.controllers
|
||||
|
||||
import android.os.StrictMode
|
||||
import android.util.Log
|
||||
import com.example.mathseduc.models.Lobby
|
||||
import com.example.mathseduc.models.Question
|
||||
import com.example.mathseduc.models.Utiliser
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.Response
|
||||
import org.json.JSONObject
|
||||
import java.io.IOException
|
||||
|
||||
|
||||
class ControllerQuestion {
|
||||
companion object {
|
||||
fun getQuestionsForLobby(questionIds: ArrayList<String>): List<Question>? {
|
||||
val questions = ArrayList<Question>()
|
||||
|
||||
for (questionId in questionIds) {
|
||||
val question = getQuestionById(questionId)
|
||||
if (question != null) {
|
||||
questions.add(question)
|
||||
}
|
||||
}
|
||||
|
||||
return if (questions.isNotEmpty()) questions else null
|
||||
}
|
||||
|
||||
private fun getQuestionById(questionId: String): Question? {
|
||||
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
|
||||
StrictMode.setThreadPolicy(policy)
|
||||
|
||||
// Client HTTP API
|
||||
val client = OkHttpClient()
|
||||
|
||||
// API Access
|
||||
val request = Request.Builder()
|
||||
.url("https://trusting-panini.87-106-126-109.plesk.page/api/questions/$questionId/qUOGkWdoPCgbmuqxIC8xiaX0rV1Pw1LoPafkaoHOgszEyD9P2vcOu493xCDZpAqO")
|
||||
.build()
|
||||
|
||||
// API Response
|
||||
client.newCall(request).execute().use { response ->
|
||||
if (!response.isSuccessful) {
|
||||
// Handle error, log, or throw an exception as needed
|
||||
return null
|
||||
}
|
||||
|
||||
// Gson deserialization
|
||||
val gson = Gson()
|
||||
val typeTokenProduct = object : TypeToken<ArrayList<Question>>() {}.type
|
||||
|
||||
val question: ArrayList<Question> = gson.fromJson(response.body!!.string(), typeTokenProduct)
|
||||
|
||||
// Assuming the API returns a list with a single question
|
||||
return if (question.isNotEmpty()) question[0] else null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.example.mathseduc.models
|
||||
|
||||
data class Question(
|
||||
val id: Int,
|
||||
val content: String,
|
||||
val nbfails: Int,
|
||||
val difficulty: Int,
|
||||
val idchapter: Int,
|
||||
val idanswergood: Int,
|
||||
)
|
Loading…
Reference in new issue