简体   繁体   中英

Post JSON Array to RetroFit 2 using Moshi

I need to send my request parameters to the Retrofit 2 API call. My request packet should be sent as followed:

REQUEST PACKET:
{
    "msgLang": "EN",
    "recieverMsisdn": [
        {
            "msisdn": "1234"
        },
        {
            "msisdn": "5678"
        }
    ],
    "textmsg": "Hello World",
    "senderName": "333111222",
    "orderKey": "orderSent"
}

I am using kotlin and retrofit for the POST request as:

val sendBulkSMSRequest = SendBulkSMSRequest(
            selectedLanguageKey,
            jsonArrayOfSelectedUsers,
            bulkSMSMessage,
            picMsisdn,
            ConstantsUtility.OrderManagementKeys.BULK_SMS
        )

        var mObserver = mUsersSelectionRepository.requestSendBulkSMS(sendBulkSMSRequest)
        mObserver = mObserver.onErrorReturn { null }

        disposable = mObserver
            .compose(applyIOSchedulers())
            .subscribe(
                { result ->
                    view()?.hideLoading()
                    view()?.isShowErrorPopup(result)?.let {
                        if (!it) {
                            sendBulkSMSResponseLiveData.postValue(result)
                        }
                    }
                },
                { error ->
                    view()?.hideLoading()
                    view()?.loadError(error)
                })

        compositeDisposables!!.add(disposable)

I just want to post the request packet that is composed of some strings and an JSONArray. So far this code compiles but the API call throws error without any description or details. For the Request Class, I am doing this like:

data class SendBulkSMSRequest(
    @Json(name = "msgLang")
    val msgLang: String? = null,

    @Json(name = "recieverMsisdn")
    val recieverMsisdn: JSONArray? = null,

    @Json(name = "textmsg")
    val textmsg: String? = null,

    @Json(name = "senderName")
    val senderName: String? = null,

    @Json(name = "orderKey")
    val orderKey: String? = null
)

You should try like this:

REQUEST PACKET:
{
    "msgLang": "EN",
    "recieverMsisdn": [
        "1234",
        "5678"
    ],
    "textmsg": "Hello World",
    "senderName": "333111222",
    "orderKey": "orderSent"
}

And your model should be

data class SendBulkSMSRequest(
    @Json(name = "msgLang")
    val msgLang: String? = null,

    @Json(name = "recieverMsisdn")
    val recieverMsisdn: List<String>? = null,

    @Json(name = "textmsg")
    val textmsg: String? = null,

    @Json(name = "senderName")
    val senderName: String? = null,

    @Json(name = "orderKey")
    val orderKey: String? = null
)

Or if you need to stick with your REQUEST PACKET, then try:

data class SendBulkSMSRequest(
    @Json(name = "msgLang")
    val msgLang: String? = null,

    @Json(name = "recieverMsisdn")
    val recieverMsisdn: List<Msisdn>? = null,

    @Json(name = "textmsg")
    val textmsg: String? = null,

    @Json(name = "senderName")
    val senderName: String? = null,

    @Json(name = "orderKey")
    val orderKey: String? = null
)

and create Msisdn data model

data class Msisdn(
    @Json(name = "msisdn")
    val msisdn: String? = null
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM