首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >response.body().getBasketShopList是空的,但是邮递员中的API不是空的

response.body().getBasketShopList是空的,但是邮递员中的API不是空的
EN

Stack Overflow用户
提问于 2019-03-05 17:34:10
回答 1查看 789关注 0票数 0

我是Android的新手,我每天花3个小时在这个问题上,但我仍然找不到解决方案,我要从服务器获得一个对象列表,并将它们传递给Adapter和另一个进程。但是我遇到了麻烦,没有错误,在我的Android中,我得到了“response.code = 200”,但是我的对象列表是空的,尽管在具有相同授权和相同用户名的邮递员中,对象列表不是空的。我不知道该怎么做,所以最后我不得不问我的问题听到了。

首先让我们看看邮递员

机构:

授权:

现在,当我点击“邮递员”中的“发送按钮”时,我得到了“代码: 200”,听到的是响应主体:

代码语言:javascript
复制
{
    "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接口中:

代码语言:javascript
复制
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课程:

代码语言:javascript
复制
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类:

代码语言:javascript
复制
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;
    }

}

我的主计长,包括改装:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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,我确信在邮递员中一切都是一样的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-06 12:49:27

一个星期后,我找到了一个解决方案,我只是从我的模型(BasketShop类)操作中将一个变量“浮动计数”更改为“字符串计数”!

代码语言:javascript
复制
@SerializedName("count")
private String count;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55008519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档