&quot“ httpcode:400,errormessage:请求身体畸形”打电话给API帖子时

发布于 2025-02-01 12:21:00 字数 3029 浏览 2 评论 0原文

我正在使用Kotlin进行Android应用程序。 调用API时,我面临以下错误。 {“状态”:400,“ errormessage”:“请求身体畸形。”}

我认为消息部分是问题。

我将变量消息1和Messages2值提供给消息,但无济于事。 (RetrofitService.SendSms值也已更改以匹配该类型)

请帮助我

这是API指南。

  1. api url& API标头
  2. 请求正文
  3. 响应主体

接下来是我使用的代码。

< mainActivity.kt>

    var messages1 = listOf(messages("0"+PhoneNum_to))
    var messages2 = """{"to": "${"0"+PhoneNum_to}"}"""
    timestamp = System.currentTimeMillis().toString()
    smsservice.sendsms(
        Content_Type = "application/json; charset=UTF-8",
        timestamp = timestamp,
        accesskey = "{accesskey}",
        signature = "{signKey}",
        type = "LMS",
        contentType = "COMM",
        from = "{Caller number}",
        content = "{message content}",
        messages = messages1
)?.enqueue(object : Callback<sendsms_Response> {
    override fun onResponse(call: Call<sendsms_Response>, response: Response<sendsms_Response>) {
        if(response.isSuccessful){
            // true
        } else{
            // false
    }

    override fun onFailure(call: Call<sendsms_Response>, t: Throwable) {
        // Exception
        Log.d("NaverSMSApiResult", "onFailure 에러: " + t.message.toString());
    }
})

&lt; reatrofitservice.kt&gt;

interface RetrofitService {
    // POST 예제
    @FormUrlEncoded
    @POST("/sms/v2/services/{Service_ID}/messages")
    fun sendsms(
        @Header("Content-Type") Content_Type: String,
        @Header("x-ncp-apigw-timestamp") timestamp: String,
        @Header("x-ncp-iam-access-key") accesskey: String,
        @Header("x-ncp-apigw-signature-v2") signature: String,
        @Field("type") type: String,
        @Field("contentType") contentType: String,
        @Field("from") from: String,
        @Field("content") content: String,
        @Field("messages") messages : List<messages>
    ): Call<sendsms_Response>
}

data class messages(
    var to : String,
    var subject : String,
    var content : String,
) {
    constructor(Number : String) : this(Number, "null", "null")
}

&lt; sendss_response.kt&gt;

// DTD Create
data class sendsms_Response(
    @SerializedName("statusCode") var statusCode: String,
    @SerializedName("statusName") var statusName: String,
    @SerializedName("requestId") var requestId: String,
    @SerializedName("requestTime") var requestTime: String,
)

I am making an android application with kotlin.
I am facing the following error when calling the api.
{"status":400,"errorMessage":"request body malformed."}

I think the message part is the problem.

I gave the variables messages1 and messages2 values to messages, but to no avail. (RetrofitService.sendsms value was also changed to match the type)

please help me

Here is the api guide.

  1. API Url & API Header
    enter image description here
  2. Request body
    enter image description here
  3. Response body
    enter image description here

Next is the code I used.

<MainActivity.kt>

    var messages1 = listOf(messages("0"+PhoneNum_to))
    var messages2 = """{"to": "${"0"+PhoneNum_to}"}"""
    timestamp = System.currentTimeMillis().toString()
    smsservice.sendsms(
        Content_Type = "application/json; charset=UTF-8",
        timestamp = timestamp,
        accesskey = "{accesskey}",
        signature = "{signKey}",
        type = "LMS",
        contentType = "COMM",
        from = "{Caller number}",
        content = "{message content}",
        messages = messages1
)?.enqueue(object : Callback<sendsms_Response> {
    override fun onResponse(call: Call<sendsms_Response>, response: Response<sendsms_Response>) {
        if(response.isSuccessful){
            // true
        } else{
            // false
    }

    override fun onFailure(call: Call<sendsms_Response>, t: Throwable) {
        // Exception
        Log.d("NaverSMSApiResult", "onFailure 에러: " + t.message.toString());
    }
})

<RetrofitService.kt>

interface RetrofitService {
    // POST 예제
    @FormUrlEncoded
    @POST("/sms/v2/services/{Service_ID}/messages")
    fun sendsms(
        @Header("Content-Type") Content_Type: String,
        @Header("x-ncp-apigw-timestamp") timestamp: String,
        @Header("x-ncp-iam-access-key") accesskey: String,
        @Header("x-ncp-apigw-signature-v2") signature: String,
        @Field("type") type: String,
        @Field("contentType") contentType: String,
        @Field("from") from: String,
        @Field("content") content: String,
        @Field("messages") messages : List<messages>
    ): Call<sendsms_Response>
}

data class messages(
    var to : String,
    var subject : String,
    var content : String,
) {
    constructor(Number : String) : this(Number, "null", "null")
}

<sendsms_Response.kt>

// DTD Create
data class sendsms_Response(
    @SerializedName("statusCode") var statusCode: String,
    @SerializedName("statusName") var statusName: String,
    @SerializedName("requestId") var requestId: String,
    @SerializedName("requestTime") var requestTime: String,
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹梦 2025-02-08 12:21:00

错误是因为您的消息数据无法识别为JSON,并且在身体中存在问题。尝试以正确的JSON表格进行修复,我认为这将解决您的问题。

另外,您可以查看此链接以获取更多信息:

https://www.ktmbytes.com/send-sms-from-android-app-using-nexmo-and-retrofit

The error is caused because your message data cannot be recognized as a JSON and has a problem in the body. Try to fix it in a correct JSON form and I think that will solve your problem.

also you can check out this link for more info:

https://www.ktmbytes.com/send-sms-from-android-app-using-nexmo-and-retrofit

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文