繁体   English   中英

使用带有行 json 数据 kotlin 的 Retrofit2 发送发布请求

[英]send post request using Retrofit2 with row json data kotlin

我正在尝试向支付服务器 payex 发送发布请求,当我尝试使用 postman 发送请求时它工作正常,但抛出 android 不工作我得到 retrofit2.HttpException: HTTP 400 Bad Request 据我了解问题可能正在发送数据为行 json

这是我的代码

// ViewModel

val mediaType: MediaType? = "application/json".toMediaTypeOrNull()
        val body: RequestBody = RequestBody.create(
            mediaType,
            "  {\n        \"amount\": 1000,\n        \"currency\": \"MYR\",\n        \"collection_id\": \"\",\n        \"capture\": true,\n        \"customer_name\": \"Dholfaqar\",\n        \"email\": \"udalharazi@gmail.com\",\n        \"contact_number\": \"0172572068\",\n        \"address\": \"eco sky resident\",\n        \"postcode\": \"56473\",\n        \"city\": \"kuala lumpuer\",\n        \"state\": \"kuala lumpuer\",\n        \"country\": \"malaysia\",\n        \"shipping_name\": \"\",\n        \"shipping_email\": \"\",\n        \"shipping_contact_number\": \"\",\n        \"shipping_address\": \"\",\n        \"shipping_postcode\": \"\",\n        \"shipping_city\": \"\",\n        \"shipping_state\": \"\",\n        \"shipping_country\": \"\",\n        \"description\": \"testing\",\n        \"reference_number\": \"122674\",\n        \"payment_type\": \"card\",\n        \"show_payment_types\": false,\n        \"tokenize\": false,\n        \"card_on_file\": \"\",\n        \"return_url\": \"\",\n        \"callback_url\": \"\",\n        \"accept_url\": \"\",\n        \"reject_url\": \"\",\n        \"nonce\": \"\",\n        \"source\": \"androidapp\",\n        \"expiry_date\": \"2022-11-25\"\n    }\n"
        )

        Log.d("body info : ","info => "+body.toString())
        GlobalScope.async(Dispatchers.IO) {
            try {

                val retrofit = ServiceBuilder.buildService(AppApis::class.java)
                retrofit.PaymentIntents("Bearer eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiYWRyaWFuQG9wZW5rb20uaW8iLCJNZXJjaGFudElkIjoiMTE3MSIsIk1JRCI6IjEwMTc4MDEiLCJjdG9zIjoidHJ1ZSIsInBhcnRuZXJzIjoidHJ1ZSIsImV4cCI6MTY2OTk1NjA1OCwiaXNzIjoicGF5ZXguaW8iLCJhdWQiOiJwYXlleC5pbyJ9.SrhMtRA39OcprOeLXxyCOYk8c8r8jVAFPzrjiob0u9A",
                    "application/json","application/json",body).enqueue(
                    object : Callback<ResponseBody> {
                        override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {

                            var res = response.body()?.string()
                            Log.d("response xxxxx 43","response => "+res.toString())
                            var resobj = JSONObject(res!!)


                        }

                        override fun onFailure(
                            call: Call<ResponseBody>,
                            t: Throwable
                        ) {
                        }


                    })
            }catch (e: Exception) {
                Log.d("outletresponse","Exception => "+ e.toString())
            }
        }
// AppApis

// @FormUrlEncoded
    @POST(AppConstants.Get_Payment_Intents)
    suspend fun PaymentIntents(
        @Header("Authorization") Authorization: String,
        @Header("accept") accept: String,
        @Header("Content-Type") Content_Type: String,
        @Body bodyParameters:
        RequestBody,
    ): Call<ResponseBody>

对于 body 的数据类型,我尝试了很多不同的方法,但都没有用,这就是我尝试的

val request = PaymentRequest()
        request.amount =  amount.toString()
        request.currency =  currency
        request.collection_id =  collection_id
        request.capture =  capture.toString()
        request.customer_name =  customer_name
        request.email =  email
        request.contact_number =  contact_number
        request.address =  address
        request.postcode =  postcode
        request.city =  city
        request.state =  state
        request.country =  country
        request.shipping_name =  shipping_name
        request.shipping_email =  shipping_email
        request.shipping_contact_number =  shipping_contact_number
        request.shipping_address =  shipping_address
        request.shipping_postcode =  shipping_postcode
        request.shipping_city =  shipping_city
        request.shipping_state =  shipping_state
        request.shipping_country =  shipping_country
        request.description =  description
        request.reference_number =  reference_number
        request.payment_type =  payment_type
        request.show_payment_types =  show_payment_types.toString()
        request.tokenize =  tokenize.toString()
        request.card_on_file =  card_on_file
        request.return_url =  return_url
        request.callback_url =  callback_url
        request.accept_url =  accept_url
        request.reject_url =  reject_url
        request.nonce =  nonce
        request.source =  source
        request.expiry_date =  expiry_date 
    ```

 ``` val params2: MutableMap<String, String> = HashMap() ```

 val bodyParameters = JsonObject()



this is the curl in postman which is working fine 

curl --location --request POST 'https://sandbox-payexapi.azurewebsites.net/api/v1/PaymentIntents' \
--header 'accept: application/json' \
--header 'Authorization: Bearer eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiYWRyaWFuQG9wZW5rb20uaW8iLCJNZXJjaGFudElkIjoiMTE3MSIsIk1JRCI6IjEwMTc4MDEiLCJjdG9zIjoidHJ1ZSIsInBhcnRuZXJzIjoidHJ1ZSIsImV4cCI6MTY2OTk1NjA1OCwiaXNzIjoicGF5ZXguaW8iLCJhdWQiOiJwYXlleC5pbyJ9.SrhMtRA39OcprOeLXxyCOYk8c8r8jVAFPzrjiob0u9A' \
--header 'Content-Type: application/json' \
--header 'Cookie: ARRAffinity=6a0fcede443e754c9c62fee8eaa769c00469246725bdb526671819e1553ae727; ARRAffinitySameSite=6a0fcede443e754c9c62fee8eaa769c00469246725bdb526671819e1553ae727' \
--data-raw '[
    {
        "amount": 1000,
        "currency": "MYR",
        "collection_id": "",
        "capture": "true",
        "customer_name": "Dholfaqar",
        "email": "udalharazi@gmail.com",
        "contact_number": "0172572068",
        "address": "eco sky resident",
        "postcode": "56473",
        "city": "kuala lumpuer",
        "state": "kuala lumpuer",
        "country": "malaysia",
        "shipping_name": "",
        "shipping_email": "",
        "shipping_contact_number": "",
        "shipping_address": "",
        "shipping_postcode": "",
        "shipping_city": "",
        "shipping_state": "",
        "shipping_country": "",
        "description": "testing",
        "reference_number": "122674",
        "payment_type": "card",
        "show_payment_types": false,
        "tokenize": false,
        "card_on_file": "",
        "return_url": "",
        "callback_url": "",
        "accept_url": "",
        "reject_url": "",
        "nonce": "",
        "source": "androidapp",
        "expiry_date": "2022-11-25"
    }
]'

您需要为键添加 SerializedName 注释,以便当我们使用 Retrofit 时,它会使用这些键生成正确的 json。 使用 SerializedName 我们可以将我们的属性 map 转换为 jason 键。 请记住,最好将属性设为可选,这样即使服务器没有响应所有密钥,我们仍然可以在客户端创建丰富的 object。

例如:

data class UserInfo (
@SerializedName("user_id") val userId: Int?,
@SerializedName("user_name") val userName: String?,
@SerializedName("user_email") val userEmail: String?,
@SerializedName("user_age") val userAge: String?,
@SerializedName("user_uid") val userUid: String?
)

暂无
暂无

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

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