问题描述
我是Android的新手,大约一周的时间,我每天要花3个小时来解决这个问题,但仍然找不到解决方案,我将从服务器中获取对象列表并将它们传递给Adapter和另一个过程。但是我遇到了麻烦,没有错误,在我的Android Studio中,我得到了 response.code = 200,但是我的对象列表为空,尽管在具有相同授权和相同用户名的Postman中,对象列表并不为空。我不知道该怎么办,所以最后我不得不问我的问题。
I am new to Android, it's about a week that I am spending 3 hours a day on this problem but still I can not find a solution, I am going to get a list of object from server and pass them to Adapter and another process. But I got into trouble, there is no error, in my Android Studio I got " response.code = 200 " but a list of my object is empty although in Postman with same authorization and same username a list of object is not empty. I don't know what should I do, so finally I forced to ask my question hear.
首先让我们看看邮递员
正文::
Body : :
授权::
Authorization : :
现在,当我在邮递员中单击发送按钮时,我得到了代码:200,并且听到的是响应正文:
Now when I clicked on Send Button in Postman I got "code: 200" and hear is the response Body:
{
"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界面中:
In my 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类:
My MainBasketShopResponse class:
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类:
BasketShop class:
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;
}
}
包含改造的我的控制器:
My Controller that contain retrofit:
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());
}
});
}
}
听觉是我的BasketShopFragment的一部分,我称之为MainBasketShopController
Hear is a part of my BasketShopFragment that I call MainBasketShopController with it:
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();
}
};
我检查了我传递给控制器的用户名和accessToken,并确定一切都在寻找就像在邮递员中一样
I checked both of username and accessToken that i passed to the controller and I am sure that everything is looking like in Postman
推荐答案
一周后,我找到了一个解决方案,我只是将变量 float count更改为 String count
After a week I found a solution , I just changed a variable "float count" to "String count" from my Model(BasketShop Class) Ops!
@SerializedName("count")
private String count;
这篇关于response.body()。getBasketShopList为空,但Postman中的API JSON不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!