Android volley Timeout Exception when using RequestFuture.get()

后端 未结 2 2120
轻奢々
轻奢々 2021-02-19 21:25

In my Fragment, i am trying to use TMDB\'s open movie DB to get details about \"Now Playing\" Movies.

If i use RequestFuture.get(time, TimeUnit) method to execute this v

2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-19 21:57

    Sad that no-one could help answer this question but i managed to solve this issue like below:

    The timeout will happen to the RequestFuture.get() if it is on the same thread as the UI thread. I have changed the mechanism of the request so that the request is done on a separate Asynch thread(not UI thread) and the response is also received on a separate thread from the request like below:

    private void StepA() {
            Log.d("RT", "StepA initiated");
            final CountDownLatch latchA = new CountDownLatch(1);
    
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    Log.d("RT", "Thread t Begins");
                    ThreadA threadA = new ThreadA();
                    try {
                        JSONObject jsonObject = threadA.execute().get(10, TimeUnit.SECONDS);
                        parseA(jsonObject);
                        latchA.countDown();
                        Log.d("RT", "Thread t Ends");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    } catch (TimeoutException e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();
            try {
                latchA.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Log.d("RT", "StepA END");
        }
    

    Below is the Asynch task code for the request:

    protected class ThreadA extends AsyncTask {
        final String url = mBuilder.getURL("box");
    
        public ThreadA() {
        }
    
        @Override
        protected JSONObject doInBackground(Void... params) {
            final RequestFuture future = RequestFuture.newFuture();
            JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, (String) null, future, future);
            requestQueue.add(request);
            try {
                return future.get(10, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
    

    I've added countdown latches cause they are awesome and also cause i have few more requests like this in my program that depend on this snippet's response. Hence they help run the program more synchronously.

提交回复
热议问题