改造入队方法未被调用

时间:2017-07-21 20:10:24

标签: android callback retrofit2

我正在进行异步调用并且能够向服务器发送请求并获取我想要的JSON(使用邮递员),但由于某种原因,OnResponse和OnFailure方法没有被执行,如果我错过了,请你帮帮我something.Im使用retrofit2

private List<Advert> getAdverts(){

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl( "http://192.168.100.4:8092/portal/")
            .client(client.build())
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    RequestService requestService = retrofit.create(RequestService.class);

    Call<List<Advert>> customerCall= requestService.getAdverts(accountNumber);
    System.out.println("++++++++++++++ request was made +++++++++++++++++++");


    customerCall.enqueue(new Callback<List<Advert>>() {
        @Override
        public void onResponse(Call<List<Advert>> call, Response<List<Advert>> response) {
            Log.d("onResponse", "" + response.message());

            System.out.println("++++++++++++++ was here+++++++++++++++++++");

            if (response.isSuccessful()) {
                adverts.addAll(response.body());

            } else {
                Toast.makeText(getContext(), "" + response.message(), Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void onFailure(Call<List<Advert>> call, Throwable t) {
            Log.d("onFailure", t.toString());
        }
    });

    return adverts;
}

1 个答案:

答案 0 :(得分:0)

不确定,但是在de操作结束之前你已经返回了(正如你提到的那样是异步调用)。所以,一旦你排队,你就会回来&#34;广告&#34; (可能取消你的电话)。

考虑使用其他替代方法来返回您的响应,例如Callbacks,EventBus,将响应保留在本地存储上等等。