简体   繁体   English

使用 Moshi 将 JSON 阵列发布到 RetroFit 2

[英]Post JSON Array to RetroFit 2 using Moshi

I need to send my request parameters to the Retrofit 2 API call.我需要将我的请求参数发送到 Retrofit 2 API 调用。 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:我使用 kotlin 和 retrofit 作为 POST 请求:

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.我只想发布由一些字符串和 JSONArray 组成的请求数据包。 So far this code compiles but the API call throws error without any description or details.到目前为止,此代码已编译,但 API 调用抛出错误,没有任何描述或详细信息。 For the Request Class, I am doing this like:对于请求 Class,我这样做:

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你的 model 应该是

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:或者,如果您需要坚持使用 REQUEST PACKET,请尝试:

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并创建Msisdn数据 model

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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