在使用OkHttp调用多个异步请求时,有时回调会受到干扰

时间:2016-07-03 15:39:21

标签: android okhttp

我在CustomerService类上有两个方法,当我在HomeFragment上调用它们时,五次之一,我看到在getTransactions回调中我得到了getCard服务响应

public class CustomerService {

    OkHttpClient okHttpClient = new OkHttpClient();

    .....

    public void getTransactions(String memberId, Callback callback)
    {
        List<Pair> params = new ArrayList<>();
        params.add(new Pair<>("member_id", memberId));

        String url = BASE_URL + "customer/gettransaction/?" + getQuery(params);
        Request request = new Request.Builder().url(url).build();
        okHttpClient.newCall(request).enqueue(callback);
    }

    public void getCard(String memberId, Callback callback)
    {
        List<Pair> params = new ArrayList<>();
        params.add(new Pair<>("member_id", memberId));

        String url = BASE_URL + "customer/getcard/?" + getQuery(params);
        Request request = new Request.Builder().url(url).build();

        okHttpClient.newCall(request).enqueue(callback);
    }

}

在片段类中,我将它们称为如下:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.fragment_activities, container, false);
    realm = Realm.getDefaultInstance();

    getCardData();
    getTransactions();

    ....

    return rootView;
}

private void getTransactions() {
    final User currentUser = UserRepository.getUser(realm);

    customerService.getTransactions(currentUser.getMember_id(), new Callback() {
        @Override
        public void onFailure(Call call, final IOException e) {

            Log.d(TAG, "Transaction service error: " + e.getMessage());
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {

            Type responseType = new TypeToken<WsResponse<List<WsTransaction>>>() {}.getType();
            final List<WsTransaction> wsTransactionList = LoyaltyService.getWsResponseData(response, responseType);

            if (wsTransactionList != null) {

               ....
            }
        }
    });
}

private void getCardData() {
    final User currentUser = UserRepository.getUser(realm);

    customerService.getCard(currentUser.getMember_id(), new Callback() {
        @Override
        public void onFailure(Call call, final IOException e) {

            Log.d(TAG, "Customer - GetCard service error: " + e.getMessage());
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {

            Type responseType = new TypeToken<WsResponse<WsCard>>() {}.getType();
            final WsCard wsCard = LoyaltyService.getWsResponseData(response, responseType);
            if (wsCard != null) {

                .....
            }
        }
    });
}

如果我延迟调用getTransactions半秒钟,那么它没有问题。或者当我调试时,我没有任何问题。有人可以帮我理解我错过的东西吗?

2 个答案:

答案 0 :(得分:0)

带回调的入队是异步的,所以很可能你使用相同的回调但不等待登录成功。如果是这种情况并且您期望某个结果顺序,那么第二个getCard请求很可能在登录之前完成。

需要更多背景来确认是否可能出现这种情况。

答案 1 :(得分:0)

原来是服务器方面的问题。