android 使用Retrofit2和数据Kotlin上的行json发送发布请求

zlhcx6iw  于 2022-12-09  发布在  Android
关注(0)|答案(1)|浏览(295)

我试图发送后请求到支付服务器payex,当我尝试发送请求使用 Postman 它的工作正常,但抛出的Android是不工作我得到retrofit2.HttpException:HTTP 400错误请求,据我所知,问题可能是将数据作为行json发送
这是我密码
第一个
对于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"
    }
]'
4c8rllxm

4c8rllxm1#

你需要为键添加SerializedName注解,这样当我们使用Retrofit时,它会用这些键生成正确的json。使用SerializedName,我们可以将属性Map到json键。记住,最好将属性设置为可选,这样即使服务器没有用所有键响应,我们仍然可以在客户端创建富对象。
例如:

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?
)

相关问题