Reputation: 29
I'm Struggling how to send POST with json using Retrofit2. like this:
{
"user_available_id": 702,
"teacher_id" : 3207,
"schedule" : [{
"event_id" : 47533,
"schedule_time" : "2020-11-30 07:00:00",
"status" :1
},
{
"event_id" : 47532,
"schedule_time" : "2020-11-30 06:30:00",
"status" :1
}]
}
I'm suppose send post like that. And I wonder is it possible to send like that or there is another way. can you kindly tell me if there is another way. btw here's how I try to send it
CreateSchduleAPI.kt
@POST("schedule/student-create")
@Headers("Accept: application/json")
@SerializedName("data")
suspend fun createScheduleSesi2(
@Header("Authorization") token: String?,
@Body createSchedule: String
): Response<ScheduleModel>
And the Model ScheduleModel.kt
@Parcelize
data class ScheduleModel(
@field:SerializedName("user_available_id")
var userAvailableId: String? = null,
@field:SerializedName("schedule")
var schedule: ArrayList<schedule?>? = null,
@field:SerializedName("teacher_id")
var teacherId: String? = null
) : Parcelable
@Parcelize
data class schedule(
@field:SerializedName("event_id")
var eventId: String? = null,
@field:SerializedName("schedule_time")
var scheduleTime: String? = null,
@field:SerializedName("status")
var status: String? = null
) : Parcelable
First
private suspend fun getMultiSlotJadwal(id: String, date: String) {
jamList.clear()
val networkConfig =
NetworkConfig().getTeacher().getTeacherScheduleAvailability(token, id, date)
if (networkConfig.isSuccessful) {
if (networkConfig.body()!!.availability!!.isEmpty()) {
binding.rvSlot.visibility = View.GONE
Handler(Looper.getMainLooper()).post {
Toast.makeText(
this,
"Jam tidak tersedia",
Toast.LENGTH_SHORT
).show()
}
} else {
for (slot in networkConfig.body()!!.availability!!) {
//convert tanggal start ke millis
val tanggalSlot = slot!!.start!!.toDate().formatTo("yyyy-MM-dd HH:mm")
val tanggalInMillis = convertToMillis(tanggalSlot)
//ambil tanggal sekarang
val myFormat = "yyyy-MM-dd HH:mm" // format tanggal
val calendar = Calendar.getInstance()
val time = calendar.time
val sdf = SimpleDateFormat(myFormat, Locale.getDefault())
val curdate = sdf.format(time) //diconvert ke tanggal local
val curDateinMillis = convertToMillis(curdate) // convert ke millis
val hasilDate = tanggalInMillis - curDateinMillis
val tanggalJam = hasilDate / 3600000 //diubah dari millis ke jam
if (tanggalJam >= 6) {
jamList.add(slot)
val sortJamList = jamList.sortedBy { jamList -> jamList.start }
binding.rvSlot.visibility = View.VISIBLE
binding.rvSlot.adapter = SlotJamAdapter(sortJamList) {
teacher_id = it.teacherId.toString()
scheduleModel.teacherId = teacher_id
scheduleModel.userAvailableId = user_avalaible_id
scheduleItem.scheduleTime = it.start.toString()
scheduleItem.status = "1"
scheduleItem.eventId = it.id.toString()
scheduleList.add(scheduleItem)
scheduleModel.schedule = scheduleList
itemClicked = true
changeBackgroundButtonSesi2()
}
}
}
}
} else {
Handler(Looper.getMainLooper()).post {
Toast.makeText(
this,
"Jam tidak tersedia",
Toast.LENGTH_SHORT
).show()
}
}
}
Second
private suspend fun createSchedule2Sesi() {
val jsonSchedule = Gson().toJson(scheduleModel)
val networkConfig = NetworkConfig().createSchedule().createScheduleSesi2(
token,
jsonSchedule
)
try {
if (networkConfig.isSuccessful) {
Handler(Looper.getMainLooper()).post {
Toast.makeText(
this,
"Pembuatan Jadwal Berhasil",
Toast.LENGTH_LONG
).show()
startActivity(Intent(this, MainActivity::class.java))
finish()
}
} else {
Handler(Looper.getMainLooper()).post {
Toast.makeText(
this,
"Pembuatan Jadwal Gagal, Cek Koneksi",
Toast.LENGTH_LONG
).show()
}
}
}catch (e:Exception){
Log.e(TAG, "createSchedule2Sesi: ${e.message}", )
}
}
Thank you in advance
Upvotes: 0
Views: 61
Reputation: 4323
Retrofit allows you to use your Kotlin object as a parameter of a call. It will take care of the json serialisation itself if you use GsonConverterFactory
when building your Retrofit instance.
That will allow you to change the definition of your endpoint as below
@POST("schedule/student-create")
suspend fun createScheduleSesi2(
@Header("Authorization") token: String?,
@Body createSchedule: ScheduleModel
): Response<ScheduleModel>
Upvotes: 1