Android中用Retrofit2发送Raw JSON请求至Payex服务器遇400错误
Android Retrofit 请求Payex支付服务器返回HTTP 400 Bad Request问题
我尝试向Payex支付服务器发送POST请求,Postman中请求可正常运行,但Android端用Retrofit实现时出现retrofit2.HttpException: HTTP 400 Bad Request错误,推测问题出在Raw JSON数据的发送方式上。
ViewModel代码
// 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代码
// 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>
我试过多种请求体数据类型,都无效:
- 使用PaymentRequest实体类赋值
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
- 使用HashMap
val params2: MutableMap<String, String> = HashMap()
- 使用JsonObject
val bodyParameters = JsonObject()
以下是Postman中可正常运行的Curl命令:
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" } ]'
内容的提问来源于stack exchange,提问作者Dholfaqar Mohammed Alharazi
相关产品推荐
相关产品推荐

