Reputation: 314
I am using retrofit for calling the API i have to send the data in Body as we send in Java @Body but don't know how to parse the data...
{
"appType": "EXTERNAL",
"appDetails":{
"os": "MAC_OSX",
"osVersion": "1.2",
"appVersion": "1.0",
"deviceFamily": "MOBILE",
"ipAddress": "192.168.5.2"
},
"consumerSections":[
"Support",
"English",
"other"
],
"engagementAttributes": [
{
"type": "personal",
"personal": {
"contacts": [{"email":"test.com","phone":"12345678"},{"email":"test2.co.il","phone":"98765430"}],
"age": {
"age":30.0,
"year":1985,
"month":7,
"day":22
},
"firstname": "test",
"lastname": "test2",
"gender": "FEMALE",
"company": "liveperson"
}
}
]
}
in the API call, how can i parse it and send data to the server. Please tell....
Upvotes: 3
Views: 12007
Reputation: 1
if you post method recive in the body a parameter for example {"name":"pedro"}
you must crate a dataclass
data class myPostMethodBody( val name: String )
and y the post interface method
@POST("myPostMethod") @Headers("Content-Type: application/json") suspend fun myPostMethod(@Body body: myPostMethodBody ): List
Upvotes: 0
Reputation: 1927
1. Add the below code in apiService Class:
@POST("login")
fun user_login(@Query("role") role: String?, @Body user_registering: RequestBody?): Call<LoginUserResponse?>?
2. call below func for requestBody:
private fun callLogIn2(type: String, email: String, password: String) {
binding.loader.setVisibility(View.VISIBLE)
val jsonObject = JSONObject()
try {
jsonObject.put("login_id", email)
jsonObject.put("password", password)
jsonObject.put("fcm_id",Prefs.getPrefInstance()!!.getValue(this@Login, Const.FCM_ID, ""))
} catch (e: JSONException) {
binding.loader.setVisibility(View.GONE)
e.printStackTrace()
}
val params = jsonObject.toString()
val user_logging_in = params.toRequestBody("application/json".toMediaTypeOrNull())
APIUtils.getAPIService()?.user_login(type, user_logging_in)
?.enqueue(object : Callback<LoginUserResponse?> { override fun onResponse(call: Call<LoginUserResponse?>,
response: Response<LoginUserResponse?>
) {
if (response.body() != null) {
if (response.body()!!.getStatus() != null && response.body()!!.getStatus()!!.equals(200)) {
if (response.body()!!.getData()?.get(0)!!.getPaymentStatus() == true) {
if (binding.rememberMe.isChecked()) {
Prefs.getPrefInstance()!!
.setValue(this@Login, Const.PASSWORD, password)
Prefs.getPrefInstance()!!
.setValue(this@Login, Const.KEEP_USER_LOGGED_IN, "1")
} else {
Prefs.getPrefInstance()!!.setValue(this@Login, Const.PASSWORD, "")
Prefs.getPrefInstance()!!.setValue(this@Login, Const.KEEP_USER_LOGGED_IN, "0")
}
if (isCustomer!!) startActivity(
Intent(this@Login, MainActivity::class.java).putExtra("isCustomer", true)
) else
startActivity(Intent(this@Login, MainActivity::class.java).putExtra("isCustomer", false))
} else {
binding.loader.setVisibility(View.GONE)
}
} else {
binding.loader.setVisibility(View.GONE)
}
} else {
binding.loader.setVisibility(View.GONE)
}
}
override fun onFailure(call: Call<LoginUserResponse?>, t: Throwable) {
t.printStackTrace()
binding.loader.setVisibility(View.GONE)
}
})
}
Upvotes: 0
Reputation: 1310
If you need to send plain json, you could do:
@Headers("Content-Type: application/json")
@POST("login")
fun getUser(@Body body: String) : Call<User>
If you want automatically cast you kotlin data class, add
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
And then, just set your object with @Body annot.
@Headers("Content-Type: application/json")
@POST("login")
fun getUser(@Body body: YourCustomDataObject) : Call<User>
Here is an example how to connect all your retrofit2 and service interface together.
Upvotes: 0