/** * 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(); }
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; } }
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; }
for(int requestId=1; requestId<numOfRequests; ++requestId) { try { RequestFuture<String> future = RequestFuture.newFuture(); final String nextUrl = market.getCurrencyPairsUrl(requestId); if(!TextUtils.isEmpty(nextUrl)) {
@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; }
for(int requestId=1; requestId<numOfRequests; ++requestId) { try { RequestFuture<String> future = RequestFuture.newFuture(); final String nextUrl = market.getUrl(requestId, checkerInfo); if(!TextUtils.isEmpty(nextUrl)) {
final JSONObject postData = createRequestWrapper(params, odooResponse); OdooLog.d("POST DATA: " + postData); RequestFuture<JSONObject> requestFuture = RequestFuture.newFuture(); if (backResponse == null) { Response.ErrorListener errorListener = new Response.ErrorListener() {