英文:
Kotlin Retrofit not returning json object data from an Api call
问题
我正在创建一个Kotlin Android移动应用程序,并需要调用Rest API来将客户数据传递给应用程序。我正在尝试使用Retrofit来实现这个功能。
我有一个POST接口,它将一个sessionKey的请求体传递给Rest API端点。
调用API成功了,但是我期望得到的响应却没有任何数据。该API包含许多包含数据的对象。以下是通过Postman进行Post请求时返回的JSON示例:
{
"OrderingOrderInfoResponseStruct": {
"deliveryAddresses": {
"OrderingDeliveryAddressStruct": [
{
"deliveryAddressNo": 5800009,
"deliveryAddressName": "BASILDON & THURROCK NHS",
"pointsOfService": {
"OrderingPointOfServiceStruct": [
{
"pointOfServiceNo": 1,
"pointOfServiceName": "A & E",
"pointOfServiceDescription": "A & E 12",
"pointOfServiceOrderingGroupNo": "580-1",
"orders": {
"OrderingOrderStruct": [
{
"orderType": "inventory",
"orderDate": "2023-07-07",
"deliveryDate": "2023-07-07",
"orderStatus": 0,
"articles": {
"OrderingArticleStruct": [
{
"articleNo": 109842,
"articleDescription": "ESSENTIALS TOP SHEET S",
"articleSize": "",
"articleTargetQty": 385,
"articleMinQty": 0,
"articleMaxQty": 0,
"articleIntervalQty": 0
},
{
"articleNo": 103046,
"articleDescription": "ESSENTIALS PILLOWCASE",
"articleSize": "",
"articleTargetQty": 15,
"articleMinQty": 0,
"articleMaxQty": 0,
"articleIntervalQty": 0
},
{
"articleNo": 106856,
"articleDescription": "ESSENTIALS BATH TOWEL",
"articleSize": "",
"articleTargetQty": 24,
"articleMinQty": 0,
"articleMaxQty": 0,
"articleIntervalQty": 0
},
{
"articleNo": 106482,
"articleDescription": "ESSENTIALS BLANKET THERMAL BLUE",
"articleSize": "",
"articleTargetQty": 130,
"articleMinQty": 0,
"articleMaxQty": 0,
"articleIntervalQty": 0
},
{
"articleNo": 106812,
"articleDescription": "ESSENTIALS PATIENT MODESTY GOWN",
"articleSize": "",
"articleTargetQty": 65,
"articleMinQty": 0,
"articleMaxQty": 0,
"articleIntervalQty": 0
},
{
"articleNo": 106420,
"articleDescription": "ESSENTIALS PLASTIC BAGS WHITE",
"articleSize": "",
"articleTargetQty": 75,
"articleMinQty": 0,
"articleMaxQty": 0,
"articleIntervalQty": 0
},
{
"articleNo": 106458,
"articleDescription": "ESSENTIALS BAGS WATER SOLUBLE",
"articleSize": "",
"articleTargetQty": 25,
"articleMinQty": 0,
"articleMaxQty": 0,
"articleIntervalQty": 0
}
]
}
}
]
}
}
]
}
}
]
},
"orderingGroups": {
"OrderingOrderingGroupStruct": [
{
"orderingGroupNo": "580-1",
"orderingGroupDescription": "58000009 BASILDON OPD 1"
},
{
"orderingGroupNo": "580-2",
"orderingGroupDescription": "58000009 BASILDON OPD 2"
},
{
"orderingGroupNo": "580-3",
"orderingGroupDescription": "58000009 BASILDON OPD 3"
},
{
"orderingGroupNo": "580-4",
"orderingGroupDescription": "ROYALFREE TOWER BLOCK"
},
{
"orderingGroupNo": "580-5",
"orderingGroupDescription": "5800024 ROYAL FREE OPD 2"
},
{
"orderingGroupNo": "580-6",
"orderingGroupDescription": "5800024 ROYAL FREE OPD 3"
}
]
}
}
}
从应用程序调用API是成功的,我得到了200状态和OK消息,但没有数据。
我有几个数据类,都是使用API的JSON生成的,并使用Kotlin data class from JSON文件Android Studio插件生成的。我还尝试过使用Pojo来生成相关的模型类。
以下是我的接口和Retrofit实例:
interface OrderInfoApiService1 {
@Headers("Content-Type: application/json; charset=utf-8")
@POST("OrderInfo")
suspend fun getOrderInfo(@Body orderInfoRequest: OrderingRequest): Response<SolServices>
}
suspend fun fetchOrderInfo(orderInfoRequest: OrderingRequest) {
val retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.client(clientTime)
.build()
val retrofitService = retrofit.create(OrderInfoApiService1::class.java)
}
在Logcat中我没有看到与没有数据相关的消息。
有没有人能想到我可以尝试的事情,或者有没有明显的东西我可能忽略了?
英文:
I'm creating an Kotlin Android mobile application and need to call a Rest Api to bring customer data to the app. I'm using Retrofit to attempt this function.
I have a POST interface that passes a body of a sessionKey to a Rest Api endpoint.
The call to the Api is succesful but my response is empty of any data that i'm expecting back. The Api consits of many Objects containing data. Example below of the Json that is returned when making the Post request via Postman;
{
"OrderingOrderInfoResponseStruct": {
"deliveryAddresses": {
"OrderingDeliveryAddressStruct": [
{
"deliveryAddressNo": 5800009,
"deliveryAddressName": "BASILDON & THURROCK NHS",
"pointsOfService": {
"OrderingPointOfServiceStruct": [
{
"pointOfServiceNo": 1,
"pointOfServiceName": "A & E",
"pointOfServiceDescription": "A & E 12",
"pointOfServiceOrderingGroupNo": "580-1",
"orders": {
"OrderingOrderStruct": [
{
"orderType": "inventory",
"orderDate": "2023-07-07",
"deliveryDate": "2023-07-07",
"orderStatus": 0,
"articles": {
"OrderingArticleStruct": [
{
"articleNo": 109842,
"articleDescription": "ESSENTIALS TOP SHEET S",
"articleSize": "",
"articleTargetQty": 385,
"articleMinQty": 0,
"articleMaxQty": 0,
"articleIntervalQty": 0
},
{
"articleNo": 103046,
"articleDescription": "ESSENTIALS PILLOWCASE",
"articleSize": "",
"articleTargetQty": 15,
"articleMinQty": 0,
"articleMaxQty": 0,
"articleIntervalQty": 0
},
{
"articleNo": 106856,
"articleDescription": "ESSENTIALS BATH TOWEL",
"articleSize": "",
"articleTargetQty": 24,
"articleMinQty": 0,
"articleMaxQty": 0,
"articleIntervalQty": 0
},
{
"articleNo": 106482,
"articleDescription": "ESSENTIALS BLANKET THERMAL BLUE",
"articleSize": "",
"articleTargetQty": 130,
"articleMinQty": 0,
"articleMaxQty": 0,
"articleIntervalQty": 0
},
{
"articleNo": 106812,
"articleDescription": "ESSENTIALS PATIENT MODESTY GOWN",
"articleSize": "",
"articleTargetQty": 65,
"articleMinQty": 0,
"articleMaxQty": 0,
"articleIntervalQty": 0
},
{
"articleNo": 106420,
"articleDescription": "ESSENTIALS PLASTIC BAGS WHITE",
"articleSize": "",
"articleTargetQty": 75,
"articleMinQty": 0,
"articleMaxQty": 0,
"articleIntervalQty": 0
},
{
"articleNo": 106458,
"articleDescription": "ESSENTIALS BAGS WATER SOLUBLE",
"articleSize": "",
"articleTargetQty": 25,
"articleMinQty": 0,
"articleMaxQty": 0,
"articleIntervalQty": 0
}
]
}
}
]
}
}
]
}
}
]
},
"orderingGroups": {
"OrderingOrderingGroupStruct": [
{
"orderingGroupNo": "580-1",
"orderingGroupDescription": "58000009 BASILDON OPD 1"
},
{
"orderingGroupNo": "580-2",
"orderingGroupDescription": "58000009 BASILDON OPD 2"
},
{
"orderingGroupNo": "580-3",
"orderingGroupDescription": "58000009 BASILDON OPD 3"
},
{
"orderingGroupNo": "580-4",
"orderingGroupDescription": "ROYALFREE TOWER BLOCK"
},
{
"orderingGroupNo": "580-5",
"orderingGroupDescription": "5800024 ROYAL FREE OPD 2"
},
{
"orderingGroupNo": "580-6",
"orderingGroupDescription": "5800024 ROYAL FREE OPD 3"
}
]
}
The call to the Api from the app is successful, i get a 200 status and OK message but no data.
I have several data classes, all generated using the Json from the Api and the Kotlin data class from JSON file Android studio plugin. I've also tried using Pojo to generate the relevant model classes.
Here is my interface and retrofit instance;
interface OrderInfoApiService1 {
@Headers("Content-Type: application/json; charset=utf-8")
@POST("OrderInfo")
suspend fun getOrderInfo(@Body orderInfoRequest: OrderingRequest): Response<SolServices>
}
suspend fun fetchOrderInfo(orderInfoRequest: OrderingRequest) {
val retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())// MoshiConverterFactory.create(moshi))
.baseUrl(BASE_URL)
.client(clientTime)
.build()
val retrofitService = retrofit.create(OrderInfoApiService1::class.java)
I get no message relating to no data in the Logcat.
Can anyone think of anything i could try and anything that's really obvious that i'm missing?
答案1
得分: 1
尝试在不包装为 Response<*> 的情况下设置返回数据。
interface OrderInfoApiService1 {
@Headers("Content-Type: application/json; charset=utf-8")
@POST("OrderInfo")
suspend fun getOrderInfo(@Body orderInfoRequest: OrderingRequest): SolServices
}
这将有效,因为您的函数是 `suspend` 函数,无需包装响应。
英文:
Try to set the return data without wrapping it in Response<*>
interface OrderInfoApiService1 {
@Headers("Content-Type: application/json; charset=utf-8")
@POST("OrderInfo")
suspend fun getOrderInfo(@Body orderInfoRequest: OrderingRequest): SolServices
}
It will work because your function is suspend
function and you don't need to wrap the response.
答案2
得分: 0
我设置了一个HTTP日志记录器并注意到数据正在响应中传入,所以我的问题必须出在其他地方。
我已关闭此问题并提出了另一个问题。
谢谢。
英文:
I setup a Http logger and noticed that the data is coming in on that response, so my issue must be elsewhere.
I have closed this question and Asked another.
Thank you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论