java - okHTTP POST 请求正文有很多孩子
问题描述
我正在使用运营商的收费 API,API 调用必须传递以下 JSON 格式。我正在使用 okHTTP 库。
String telNum = "+941234567";
String pbody = "{\"amountTransaction\": {\"clientCorrelator\": \"7659\",\"endUserId\": \"tel:"+telNum+"\",\"paymentAmount\": {\"chargingInformation\": {\"amount\": 1,\"currency\": \"LKR\",\"description\": \"Test Charge\"},\"chargingMetaData\": {\"onBehalfOf\": \"IdeaBiz Test\",\"purchaseCategoryCode\": \"Service\",\"channel\": \"WAP\",\"taxAmount\": \"0\",\"serviceID\": \"theserviceid\"}},\"referenceCode\": \"REF-12345\",\"transactionOperationStatus\": \"Charged\"}}";```
以下是 JSON 需要如何格式化。
{
"amountTransaction": {
"clientCorrelator": "54321",
"endUserId": "tel:+94761234567",
"paymentAmount": {
"chargingInformation": {
"amount": 1,
"currency": "LKR",
"description": "Test Charge"
},
"chargingMetaData": {
"onBehalfOf": "IdeaBiz Test",
"purchaseCategoryCode": "Service",
"channel": "WAP",
"taxAmount": "0",
"serviceID": "null"
}
},
"referenceCode": "REF-12345",
"transactionOperationStatus": "Charged"
}
}
我收到错误 400 错误请求
解决方案
试试这个会根据你的需要格式化你的身体
try {
JSONObject jsonObject = new JSONObject(pbody);
pbody=jsonObject.toString();
} catch (JSONException e) {
e.printStackTrace();
}
推荐阅读
- java - 如何在 Websphere 应用程序服务器中设置服务器级自定义错误文本
- rust - 如何使用方法“UnicodeSegmentation::graphemes”
- javascript - 删除网站上部分 img href 的脚本
- flutter - 如何使用颤振安排本地通知
- java - 尝试使用 ProcessBuilder 从命令行启动服务器
- php - 如何使用 PHP Profiler 跟踪 Mysql 慢查询日志文件条目
- computer-science - 在特定时间后仅更新数组中特定项目的最佳方法是什么?
- java - JLabel 的文本在 hitbox 的中间
- python - 在哪里添加while循环以使最小值和最大值正常工作
- git - 如何使用 AWS Code Deploy 和 AWS Code Pipeline 一次部署多个 git 存储库?