簡體   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