com.android.volley.toolbox.RequestFuture Java Examples

The following examples show how to use com.android.volley.toolbox.RequestFuture. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VolleyX.java    From VolleyX with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #2
Source File: SyncAdapter.java    From attendee-checkin with Apache License 2.0 6 votes vote down vote up
private long postCheckIn(String attendeeId, String eventId, boolean revert, String cookie) {
    RequestQueue queue = GutenbergApplication.from(getContext()).getRequestQueue();
    RequestFuture<JSONObject> future = RequestFuture.newFuture();
    queue.add(new CheckInRequest(cookie, eventId, attendeeId, revert, future, future));
    try {
        JSONObject object = future.get();
        return object.getLong("checkinTime");
    } catch (InterruptedException | ExecutionException | JSONException e) {
        Throwable cause = e.getCause();
        if (cause instanceof ServerError) {
            ServerError error = (ServerError) cause;
            Log.e(TAG, "Server error: " + new String(error.networkResponse.data));
        }
        Log.e(TAG, "Cannot sync checkin.", e);
    }
    return -1;
}
 
Example #3
Source File: StoryService.java    From hex with Apache License 2.0 6 votes vote down vote up
public Story getStory(String storyId) {
    String STORY_PATH = "/story";
    String storyPath = mApiBaseUrl + STORY_PATH + "/" + storyId;

    RequestFuture<JSONObject> future = RequestFuture.newFuture();
    JsonObjectRequest request = new JsonObjectRequest(storyPath, future, future);
    request.setRetryPolicy(RetryPolicyFactory.build());
    mRequestQueue.add(request);

    try {
        JSONObject response = future.get();
        return StoryMarshaller.marshall(response);
    } catch (Exception e) {
        return null;
    }
}
 
Example #4
Source File: StoryCollectionService.java    From hex with Apache License 2.0 6 votes vote down vote up
public List<Story> getStories(Collection collection) {
    String apiUrl = getUrlForCollection(collection);

    RequestFuture<JSONArray> future = RequestFuture.newFuture();
    JsonArrayRequest request = new JsonArrayRequest(mApiBaseUrl + apiUrl, future, future);
    request.setRetryPolicy(RetryPolicyFactory.build());

    mRequestQueue.add(request);

    try {
        JSONArray response = future.get();
        return FrontPageMarshaller.marshall(response);
    } catch (Exception e) {
        return null;
    }
}
 
Example #5
Source File: VolleyNetworkStack.java    From wasp with Apache License 2.0 6 votes vote down vote up
private Object addToQueueSync(RequestCreator requestCreator) throws Exception {
  final RequestFuture<Object> future = RequestFuture.newFuture();
  Request<Response> request = new VolleyRequest(getMethod(requestCreator.getMethod()),
      requestCreator.getUrl(), requestCreator, future) {
    @Override
    protected void deliverResponse(Response response) {
      super.deliverResponse(response);
      future.onResponse(response.getResponseObject());
    }
  };
  future.setRequest(request);
  addToQueue(request);
  int timeout = 30000;
  if (requestCreator.getRetryPolicy() != null) {
    timeout = requestCreator.getRetryPolicy().getCurrentTimeout();
  }
  return future.get(timeout, TimeUnit.MILLISECONDS);
}
 
Example #6
Source File: ApiInvoker.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
public String invokeAPI(String host, String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType, String[] authNames) throws ApiException, InterruptedException, ExecutionException, TimeoutException {
  try {
    RequestFuture<String> future = RequestFuture.newFuture();
    Request request = createRequest(host, path, method, queryParams, body, headerParams, formParams, contentType, authNames, future, future);
    if(request != null) {
       mRequestQueue.add(request);
       return future.get(connectionTimeout, TimeUnit.SECONDS);
    } else {
      return "no data";
    }
  } catch (UnsupportedEncodingException ex) {
    throw new ApiException(0, "UnsupportedEncodingException");
  }
}
 
Example #7
Source File: ApiInvoker.java    From swaggy-jenkins with MIT License 5 votes vote down vote up
public String invokeAPI(String host, String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType, String[] authNames) throws ApiException, InterruptedException, ExecutionException, TimeoutException {
  try {
    RequestFuture<String> future = RequestFuture.newFuture();
    Request request = createRequest(host, path, method, queryParams, body, headerParams, formParams, contentType, authNames, future, future);
    if(request != null) {
       mRequestQueue.add(request);
       return future.get(connectionTimeout, TimeUnit.SECONDS);
    } else {
      return "no data";
    }
  } catch (UnsupportedEncodingException ex) {
    throw new ApiException(0, "UnsupportedEncodingException");
  }
}
 
Example #8
Source File: LoginFragment.java    From Anti-recall with GNU Affero General Public License v3.0 5 votes vote down vote up
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;
    }
}
 
Example #9
Source File: VolleyXTest.java    From VolleyX with Apache License 2.0 5 votes vote down vote up
@Test
public void getRequestFuture_customRequest() throws Exception {
    VolleyX.sInited = true;
    VolleyX.setRequestQueue(mockReqeustQueue);
    TestRequest2 testRequest = new TestRequest2(Request.Method.GET, "", null);

    VolleyX.getRequestFuture(testRequest, "mListener1");
    assertThat(testRequest.mListener1, is(instanceOf(RequestFuture.class)));
    assertThat(testRequest.getErrorListener(), is(instanceOf(RequestFuture.class)));
    assertTrue(testRequest.getErrorListener() == testRequest.mListener1);
    verify(mockReqeustQueue, times(1)).add(testRequest);
}
 
Example #10
Source File: VolleyXTest.java    From VolleyX with Apache License 2.0 5 votes vote down vote up
@Test
public void getRequestFuture_Request() throws Exception {
    VolleyX.sInited = true;
    VolleyX.setRequestQueue(mockReqeustQueue);
    TestRequest testRequest = new TestRequest(Request.Method.GET, "", null);

    VolleyX.getRequestFuture(testRequest, null);
    assertThat(testRequest.mListener, is(instanceOf(RequestFuture.class)));
    assertThat(testRequest.getErrorListener(), is(instanceOf(RequestFuture.class)));
    assertTrue(testRequest.getErrorListener() == testRequest.mListener);
    verify(mockReqeustQueue, times(1)).add(testRequest);
}
 
Example #11
Source File: RestVolleyRequest.java    From RestVolley with Apache License 2.0 5 votes vote down vote up
/**
 * execute request synchronous.
 * @param clazz response data type
 * @param <DATA> response data type
 * @return {@link RestVolleyResponse}
 */
public <DATA> RestVolleyResponse<DATA> syncExecute(Class<DATA> clazz) {
    bindOkHttpClient();
    RequestFuture<RestVolleyResponse<DATA>> future = RequestFuture.newFuture();
    mVolleyRequest = new VolleyRequest(mMethod, onBuildUrl(), clazz, future, future);
    mVolleyRequest.setTag(mTag)
            .setRetryPolicy(mRetryPolicy)
            .setSequence(mSequence)
            .setShouldCache(mShouldCache)
            .setCacheEntry(mCacheEntry)
            .setRequestQueue(mRequestEngine.requestQueue)
            .setRedirectUrl(mRedirectUrl);
    mVolleyRequest.addMarker(mMarker);

    future.setRequest(mVolleyRequest);

    mRequestEngine.requestQueue.add(mVolleyRequest);

    RestVolleyResponse<DATA> response = new RestVolleyResponse<DATA>(-1, null, null, false, 0, null);
    try {
        response = future.get();
    } catch (Exception e) {
        Throwable cause = e.getCause();
        if (cause instanceof VolleyError) {
            response.exception = (Exception) cause;
            response.message = cause.getMessage();
            NetworkResponse networkResponse = ((VolleyError) cause).networkResponse;
            response.statusCode = networkResponse.statusCode;
            response.headers = networkResponse != null ? networkResponse.headers : response.headers;
            response.networkTimeMs = networkResponse != null ? networkResponse.networkTimeMs : response.networkTimeMs;
            response.notModified = networkResponse != null ? networkResponse.notModified : response.notModified;
            response.data = (DATA)mVolleyRequest.parseNetworkResponse2RVResponse(networkResponse).data;
        }
    }

    return response;
}
 
Example #12
Source File: ApiInvoker.java    From swagger-aem with Apache License 2.0 5 votes vote down vote up
public String invokeAPI(String host, String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType, String[] authNames) throws ApiException, InterruptedException, ExecutionException, TimeoutException {
  try {
    RequestFuture<String> future = RequestFuture.newFuture();
    Request request = createRequest(host, path, method, queryParams, body, headerParams, formParams, contentType, authNames, future, future);
    if(request != null) {
       mRequestQueue.add(request);
       return future.get(connectionTimeout, TimeUnit.SECONDS);
    } else {
      return "no data";
    }
  } catch (UnsupportedEncodingException ex) {
    throw new ApiException(0, "UnsupportedEncodingException");
  }
}
 
Example #13
Source File: SyncAdapter.java    From attendee-checkin with Apache License 2.0 5 votes vote down vote up
private static JSONArray getEvents(RequestQueue requestQueue, String cookie) throws
        ExecutionException, InterruptedException {
    RequestFuture<JSONArray> future = RequestFuture.newFuture();
    requestQueue.add(new GaeJsonArrayRequest(
            BuildConfig.HOST + "/v1/event/list", cookie, future, future));
    try {
        return future.get();
    } catch (ExecutionException e) {
        if (didServerReturnNull(e)) {
            return new JSONArray();
        }
        throw e;
    }
}
 
Example #14
Source File: SyncAdapter.java    From attendee-checkin with Apache License 2.0 5 votes vote down vote up
private static JSONArray getAttendees(RequestQueue requestQueue, String eventId, String cookie)
        throws ExecutionException, InterruptedException {
    RequestFuture<JSONArray> future = RequestFuture.newFuture();
    requestQueue.add(new GaeJsonArrayRequest(
            BuildConfig.HOST + "/v1/event/" + eventId + "/attendees", cookie, future, future));
    try {
        return future.get();
    } catch (ExecutionException e) {
        if (didServerReturnNull(e)) {
            return new JSONArray();
        }
        throw e;
    }
}
 
Example #15
Source File: VolleyX.java    From VolleyX with Apache License 2.0 4 votes vote down vote up
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();
}
 
Example #16
Source File: VolleyDemoFragment.java    From RxJava-Android-Samples with Apache License 2.0 3 votes vote down vote up
/**
 * 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();
}