为什么future.get()总是为Volley RequestFuture超时?

问题描述 投票:2回答:1

我正在尝试制作同步的Volley网络请求。我正在使用请求期货等待响应,但future.get()调用总是超时(无论超时设置为多长时间)。我已经单独测试了每个组件,除了使用期货之外,似乎没有任何东西导致错误。关于我在这里错了什么的任何想法?

Activity.java:persistData()

postCampaign(campaign);

Activity.java:postCampaign()

private boolean postCampaign(final Campaign c) {
    RequestFuture<String> future = RequestFuture.newFuture();
    StringRequest request = new StringRequest(Request.Method.POST, url, future, future) {
        @Override
        protected Map<String, String> getParams()
        {
            Map<String, String>  params = new HashMap<>();
            // put data
            return params;
        }
    };
    NetworkController.getInstance(this).addToRequestQueue(request);

    try {
        String response = future.get(5, TimeUnit.SECONDS);
        Log.d("Volley", "" + response);
        return !response.contains("Duplicate");
    } catch (InterruptedException|ExecutionException|TimeoutException e) {
        Log.d("Volley", "[FAILED] " + e.getClass());
        return false;
    }
}

但奇怪的是,当单步执行代码时,似乎会使用适当的响应调用RequestFuture的onResponse方法。所以似乎RequestFuture没有正确处理响应。

enter image description here

java android multithreading networking android-volley
1个回答
0
投票

我想我已经得出结论,要么Volley不能完全同步网络请求,要么至少它不值得。我最后只是在启动时显示了一个微调器并在服务器响应成功消息时在Volley请求的onResponse方法中停止了它。就用户而言,它的工作方式相同。

© www.soinside.com 2019 - 2024. All rights reserved.