public static <E> RequestFuture<E> newFuture() { return new RequestFuture<E>(); }
@Override public T get() throws InterruptedException, ExecutionException { try { return doGet(null); } catch (TimeoutException e) { throw new AssertionError(e); } }
@Override public synchronized boolean isDone() { return mResultReceived || mException != null || isCancelled(); }
/** * Converts the Asynchronous Request into a Synchronous Future that can be used to block via * {@code Future.get()}. Observables require blocking/synchronous functions * * @return JSONObject * @throws ExecutionException * @throws InterruptedException */ private JSONObject getRouteData() throws ExecutionException, InterruptedException { RequestFuture<JSONObject> future = RequestFuture.newFuture(); String url = "http://www.weather.com.cn/adat/sk/101010100.html"; JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url, future, future); MyVolley.getRequestQueue().add(req); return future.get(); }
@Override public synchronized boolean cancel(boolean mayInterruptIfRunning) { if (mRequest == null) { return false; } if (!isDone()) { mRequest.cancel(); return true; } else { return false; } }
static <T> T generateData(Request<T> request, String listernerField) throws InterruptedException, ExecutionException { if (request == null) throw new NullPointerException("request can not be null"); RequestFuture<T> future = getRequestFuture(request, listernerField); return future.get(); }
static <T> RequestFuture<T> getRequestFuture(Request<T> request, String listernerField) { if (request == null) throw new NullPointerException("request can not be null"); RequestFuture<T> future = RequestFuture.newFuture(); String listenerFieldName = TextUtils.isEmpty(listernerField)? DEFAULT_LISTENER_FIELD: listernerField; String errorListenerFieldName = DEFAULT_ERROR_LISTENER_FIELD; try { Hack.HackedClass hackedClass = Hack.into(request.getClass()); hackedClass.field(listenerFieldName).set(request, future); hackedClass.field(errorListenerFieldName).set(request, future); } catch (Hack.HackDeclaration.HackAssertionException e) { throw new IllegalStateException("the field name of your class is not correct: " + e.getHackedFieldName()); } sRequestQueue.add(request); return future; }
@Override public synchronized boolean cancel(boolean mayInterruptIfRunning) { if (mRequest == null) { return false; } if (!isDone()) { mRequest.cancel(); return true; } else { return false; } }
private boolean verifyCaptcha(String captcha) { RequestQueue queue = Volley.newRequestQueue(getContext()); RequestFuture future = RequestFuture.newFuture(); StringRequest request = new StringRequest(Request.Method.POST, "http://ar.qsboy.com/j/verifyCaptcha", future, future) { @Override protected Map<String, String> getParams() { Map<String, String> map = new HashMap<>(); map.put("phone", phone); map.put("captcha", captcha); return map; } }; queue.add(request); try { String s = (String) future.get(5, TimeUnit.SECONDS); boolean isValid = s != null && s.length() > 0; if (isValid) { Gson gson = new Gson(); gson.fromJson(s, App.User.class); } return isValid; } catch (InterruptedException | ExecutionException | TimeoutException e) { e.printStackTrace(); return false; } }
@Override public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return doGet(TimeUnit.MILLISECONDS.convert(timeout, unit)); }
@Override public synchronized boolean cancel(boolean mayInterruptIfRunning) { if (mRequest == null) { return false; } if (!isDone()) { mRequest.cancel(); return true; } else { return false; } }
@Override public synchronized boolean isDone() { return mResultReceived || mException != null || isCancelled(); }
public static <E> RequestFuture<E> newFuture() { return new RequestFuture<E>(); }
for(int requestId=1; requestId<numOfRequests; ++requestId) { try { RequestFuture<String> future = RequestFuture.newFuture(); final String nextUrl = market.getCurrencyPairsUrl(requestId); if(!TextUtils.isEmpty(nextUrl)) { DynamicCurrencyPairsVolleyNextRequest request = new DynamicCurrencyPairsVolleyNextRequest(nextUrl, future); getRequestQueue().add(request); String nextResponse = future.get(); // this will block nextPairs.clear(); market.parseCurrencyPairsMain(requestId, nextResponse, nextPairs);
@Override public T get() throws InterruptedException, ExecutionException { try { return doGet(null); } catch (TimeoutException e) { throw new AssertionError(e); } }
@Override public synchronized boolean cancel(boolean mayInterruptIfRunning) { if (mRequest == null) { return false; } if (!isDone()) { mRequest.cancel(); return true; } else { return false; } }
@Override public synchronized boolean isDone() { return mResultReceived || mException != null || isCancelled(); }
public static <E> RequestFuture<E> newFuture() { return new RequestFuture<E>(); }
@Override protected String[] doInBackground(Void... params) { if (this.currDate == null) { Log.e(TAG, "Cannot load for null date"); return null; } String cached = this.preferences.getCachedUrl(this.currDate); if (cached != null) { return new String[]{cached, this.preferences.getCachedTitle(this.currDate)}; } RequestFuture<String> future = RequestFuture.newFuture(); volleyRequestQueue.add( new StringRequest(Request.Method.GET, "https://dilbert.com/strip/" + currDate.toString(DilbertPreferences.DATE_FORMATTER) + "/", future, future) ); try { handledResponse = handleParse(future.get(15, TimeUnit.SECONDS)); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } return handledResponse; }
@Override public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return doGet(TimeUnit.MILLISECONDS.convert(timeout, unit)); }