gpt4 book ai didi

java - response.body().getBasketShopList为空,但Postman中的API JSON不为空

转载 作者:行者123 更新时间:2023-12-02 10:10:56 24 4
gpt4 key购买 nike

我是 Android 新手,大约一周了,我每天花 3 个小时来解决这个问题,但仍然找不到解决方案,我将从服务器获取对象列表并将它们传递给适配器,然后另一个过程。但我遇到了麻烦,没有错误,在我的 Android Studio 中,我得到“response.code = 200”,但我的对象列表是空的,尽管在具有相同授权和相同用户名的 Postman 中,对象列表不为空。我不知道我该怎么办,所以最后我被迫问我的问题。

首先我们来看看Postman

正文::
Body :

授权::
Authorization :

现在,当我单击 Postman 中的“发送”按钮时,我收到“代码:200”,并听到响应正文:

{
"results": [
{
"_id": "5c7e69d283c0b00001108fad",
"count": 2,
"productId": "5ba51d877246b700016ec205",
"username": "rezash",
"createdAt": "2019-03-05T12:21:38.196UTC",
"updatedAt": "2019-03-05T12:36:11.058UTC",
"ACL": {
"*": {
"read": true,
"write": true
}
}
},
{
"_id": "5c7e69d483c0b00001108fae",
"count": 4,
"productId": "5acc0f2c790c0c000132c984",
"username": "rezash",
"createdAt": "2019-03-05T12:21:40.338UTC",
"updatedAt": "2019-03-05T12:36:15.830UTC",
"ACL": {
"*": {
"read": true,
"write": true
}
}
}
]
}

在我的 OnlineShopAPI 接口(interface)中:

public interface OnlineShopAPI {

String BASE_URL = "https://api.backtory.com/";

@Headers("X-Backtory-Object-Storage-Id:5a154d2fe4b03ffa0436a535")
@HTTP(method = "POST" , path = "object-storage/classes/query/Basket" , hasBody = true)
Call<MainBasketShopResponse> mainBasketShop (
@Header("Authorization") String authorization,
@Body BasketShop basketShop
);

interface getMainBasketShop {

void onResponse(List<BasketShop> basketShopList);

void onFailure(String cause);
}
}

我的 MainBasketShopResponse 类:

public class MainBasketShopResponse {

@SerializedName("results")
List<BasketShop> basketShopList;

public MainBasketShopResponse() {
}


public List<BasketShop> getBasketShopList() {
return basketShopList;
}

public void setBasketShopList(List<BasketShop> basketShopList) {
this.basketShopList = basketShopList;
}
}

BasketShop 类:

public class BasketShop {

@SerializedName("username")
private String username;

@SerializedName("productId")
private String productId;

@SerializedName("count")
private float count;


@SerializedName("createdAt")
private String createdAt;


@SerializedName("_id")
private String id;

public String getCreatedAt() {
return createdAt;
}

public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public BasketShop(String username) {
this.username = username;
}

public BasketShop() {
}

public BasketShop(String username, String productId, float count) {
this.username = username;
this.productId = productId;
this.count = count;
}

public BasketShop(String createdAt, String id) {
this.createdAt = createdAt;
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getProductId() {
return productId;
}

public void setProductId(String productId) {
this.productId = productId;
}

public float getCount() {
return count;
}

public void setCount(float count) {
this.count = count;
}

}

我的包含改造的 Controller :

public class MainBasketShopController {

OnlineShopAPI.getMainBasketShop getMainBasketShop;

public MainBasketShopController(OnlineShopAPI.getMainBasketShop getMainBasketShop) {
this.getMainBasketShop = getMainBasketShop;
}

public void start(String authorization , BasketShop basketShop){

Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(OnlineShopAPI.BASE_URL)
.build();

OnlineShopAPI onlineShopAPI = retrofit.create(OnlineShopAPI.class);
Call<MainBasketShopResponse> call = onlineShopAPI.mainBasketShop(authorization , basketShop);
call.enqueue(new Callback<MainBasketShopResponse>() {
@Override
public void onResponse(Call<MainBasketShopResponse> call, Response<MainBasketShopResponse> response) {

if (response.isSuccessful()) {


Log.d("emptyhst1" , response.body().getBasketShopList().toString());
Log.d("emptyhst2" , Integer.toString(response.body().getBasketShopList().size()));
getMainBasketShop.onResponse(response.body().getBasketShopList());

}
}

@Override
public void onFailure(Call<MainBasketShopResponse> call, Throwable t) {

getMainBasketShop.onFailure(t.getMessage());

}
});
}
}

Hear 是我的 BasketShopFragment 的一部分,我将其称为 MainBasketShopController:

MainBasketShopController mainBasketShopController = new MainBasketShopController(getMainBasketShop);
BasketShop basketShop = new BasketShop();
basketShop.setUsername(MyPreferenceManager.getInstance(getContext()).getUsername());
mainBasketShopController.start(
"bearer " + MyPreferenceManager.getInstance(getContext()).getAccessToken() ,
basketShop
);

OnlineShopAPI.getMainBasketShop getMainBasketShop = new OnlineShopAPI.getMainBasketShop() {
@Override
public void onResponse(List<BasketShop> basketShopList) {

Log.d("emptyhst3" , basketShopList.toString());

basketShopList2.clear();
basketShopList2.addAll(basketShopList);
mainBasketShopAdapter.notifyDataSetChanged();

}

@Override
public void onFailure(String cause) {

Toast.makeText(getContext(), cause , Toast.LENGTH_SHORT).show();

}
};

我检查了传递给 Controller ​​的用户名和 accessToken,我确信一切都像 Postman 中的那样

最佳答案

一周后我找到了解决方案,我只是将模型(BasketShop Class)Ops 中的变量“float count”更改为“String count”!

@SerializedName("count")
private String count;

关于java - response.body().getBasketShopList为空,但Postman中的API JSON不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55008519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com