Why is future.get() always timing out for Volley RequestFuture?

java android multithreading networking android-volley

466 观看

1回复

47 作者的声誉

I am trying to make a synchronous Volley networking request. I am using request futures to wait on a response, but the future.get() call always times out (no matter how long the timeout is set to). I have tested every component individually and nothing seems to be causing the error other than my use of futures. Any ideas on what I've don wrong here?

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;
    }
}

Strangely enough though, when stepping through the code, it appears that the RequestFuture's onResponse method is invoked with the appropriate response. So it seems like the RequestFuture just isn't handling the response properly.

enter image description here

作者: Caleb Whittington 的来源 发布者: 2017 年 12 月 27 日

回应 1


0

47 作者的声誉

决定

I think I've come to the conclusion that either Volley is not capable of fully synchronous networking requests or at the very least it isn't worth it. I ended up just showed a spinner on start and stopped it in the Volley request's onResponse method when the server responded with a success message. As far as the user is concerned it works the same way.

作者: Caleb Whittington 发布者: 2017 年 12 月 28 日
32x32