com.android.volley.Request类的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(10.1k)|赞(0)|评价(0)|浏览(202)

本文整理了Java中com.android.volley.Request类的一些代码示例,展示了Request类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Request类的具体详情如下:
包路径:com.android.volley.Request
类名称:Request

Request介绍

[英]Base class for all network requests.
[中]所有网络请求的基类。

代码示例

代码示例来源:origin: mcxiaoke/android-volley

@Override
  public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
      throws AuthFailureError {
    mLastUrl = request.getUrl();
    mLastHeaders = new HashMap<String, String>();
    if (request.getHeaders() != null) {
      mLastHeaders.putAll(request.getHeaders());
    }
    if (additionalHeaders != null) {
      mLastHeaders.putAll(additionalHeaders);
    }
    try {
      mLastPostBody = request.getBody();
    } catch (AuthFailureError e) {
      mLastPostBody = null;
    }
    return mResponseToReturn;
  }
}

代码示例来源:origin: bumptech/glide

@Override
public void cancel() {
 Request<byte[]> local = request;
 if (local != null) {
  local.cancel();
 }
}

代码示例来源:origin: mcxiaoke/android-volley

@SuppressWarnings("unchecked")
  @Override
  public void run() {
    // If this request has canceled, finish it and don't deliver.
    if (mRequest.isCanceled()) {
      mRequest.finish("canceled-at-delivery");
      return;
    }
    // Deliver a normal response or error, depending.
    if (mResponse.isSuccess()) {
      mRequest.deliverResponse(mResponse.result);
    } else {
      mRequest.deliverError(mResponse.error);
    }
    // If this is an intermediate response, add a marker, otherwise we're done
    // and the request can be finished.
    if (mResponse.intermediate) {
      mRequest.addMarker("intermediate-response");
    } else {
      mRequest.finish("done");
    }
    // If we have been provided a post-delivery runnable, run it.
    if (mRunnable != null) {
      mRunnable.run();
    }
  }
}

代码示例来源:origin: chentao0707/SimplifyReader

@Override
  public String toString() {
    String trafficStatsTag = "0x" + Integer.toHexString(getTrafficStatsTag());
    return (mCanceled ? "[X] " : "[ ] ") + getUrl() + " " + trafficStatsTag + " "
        + getPriority() + " " + mSequence;
  }
}

代码示例来源:origin: mcxiaoke/android-volley

/**
 * Creates a new request with the given method (one of the values from {@link Method}),
 * URL, and error listener.  Note that the normal response listener is not provided here as
 * delivery of responses is provided by subclasses, who have a better idea of how to deliver
 * an already-parsed response.
 */
public Request(int method, String url, Response.ErrorListener listener) {
  mMethod = method;
  mUrl = url;
  mIdentifier = createIdentifier(method, url);
  mErrorListener = listener;
  setRetryPolicy(new DefaultRetryPolicy());
  mDefaultTrafficStatsTag = findDefaultTrafficStatsTag(url);
}

代码示例来源:origin: chentao0707/SimplifyReader

/**
 * Creates a new request with the given method (one of the values from {@link com.android.volley.Request.Method}),
 * URL, and error listener.  Note that the normal response listener is not provided here as
 * delivery of responses is provided by subclasses, who have a better idea of how to deliver
 * an already-parsed response.
 */
public Request(int method, String url, Response.ErrorListener listener) {
  mMethod = method;
  mUrl = url;
  mErrorListener = listener;
  setRetryPolicy(new DefaultRetryPolicy());
  mDefaultTrafficStatsTag = findDefaultTrafficStatsTag(url);
}

代码示例来源:origin: chentao0707/SimplifyReader

/* protected */ static HttpUriRequest createHttpRequest(Request<?> request,
    Map<String, String> additionalHeaders) throws AuthFailureError {
  switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST: {
      byte[] postBody = request.getPostBody();
      if (postBody != null) {
        HttpPost postRequest = new HttpPost(request.getUrl());
        postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
        HttpEntity entity;
        entity = new ByteArrayEntity(postBody);
        return postRequest;
      } else {
        return new HttpGet(request.getUrl());
      return new HttpGet(request.getUrl());
    case Method.DELETE:
      return new HttpDelete(request.getUrl());
    case Method.POST: {
      HttpPost postRequest = new HttpPost(request.getUrl());
      postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
      setEntityIfNonEmptyBody(postRequest, request);
      return postRequest;
      HttpPut putRequest = new HttpPut(request.getUrl());
      putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
      setEntityIfNonEmptyBody(putRequest, request);
      return putRequest;

代码示例来源:origin: mcxiaoke/android-volley

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
    throws IOException, AuthFailureError {
  String url = request.getUrl();
  HashMap<String, String> map = new HashMap<String, String>();
  map.putAll(request.getHeaders());
  map.putAll(additionalHeaders);
  if (mUrlRewriter != null) {
      connection.getResponseCode(), connection.getResponseMessage());
  BasicHttpResponse response = new BasicHttpResponse(responseStatus);
  if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
    response.setEntity(entityFromConnection(connection));

代码示例来源:origin: chentao0707/SimplifyReader

private static void addBodyIfExists(HttpURLConnection connection, Request<?> request)
      throws IOException, AuthFailureError {
    byte[] body = request.getBody();
    if (body != null) {
      connection.setDoOutput(true);
      connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType());
      DataOutputStream out = new DataOutputStream(connection.getOutputStream());
      out.write(body);
      out.close();
    }
  }
}

代码示例来源:origin: mcxiaoke/android-volley

@SuppressWarnings("deprecation")
/* package */ static void setConnectionParametersForRequest(HttpURLConnection connection,
    Request<?> request) throws IOException, AuthFailureError {
  switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST:
      byte[] postBody = request.getPostBody();
      if (postBody != null) {
        connection.setRequestMethod("POST");
        connection.addRequestProperty(HEADER_CONTENT_TYPE,
            request.getPostBodyContentType());
        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
        out.write(postBody);

代码示例来源:origin: chentao0707/SimplifyReader

int timeoutMs = request.getTimeoutMs();
client.setConnectTimeout(timeoutMs, TimeUnit.MILLISECONDS);
client.setReadTimeout(timeoutMs, TimeUnit.MILLISECONDS);
okHttpRequestBuilder.url(request.getUrl());
Map<String, String> headers = request.getHeaders();
for (final String name : headers.keySet()) {
  okHttpRequestBuilder.addHeader(name, headers.get(name));

代码示例来源:origin: mcxiaoke/android-volley

addCacheHeaders(headers, request.getCacheEntry());
  httpResponse = mHttpStack.performRequest(request, headers);
  StatusLine statusLine = httpResponse.getStatusLine();
    Entry entry = request.getCacheEntry();
    if (entry == null) {
      return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, null,
    request.setRedirectUrl(newUrl);
  attemptRetryOnException("connection", request, new TimeoutError());
} catch (MalformedURLException e) {
  throw new RuntimeException("Bad URL " + request.getUrl(), e);
} catch (IOException e) {
  int statusCode = 0;
    VolleyLog.e("Request at %s has been redirected to %s", request.getOriginUrl(), request.getUrl());
  } else {
    VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());

代码示例来源:origin: chentao0707/SimplifyReader

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
    throws IOException, AuthFailureError {
  HttpUriRequest httpRequest = createHttpRequest(request, additionalHeaders);
  addHeaders(httpRequest, additionalHeaders);
  addHeaders(httpRequest, request.getHeaders());
  onPrepareRequest(httpRequest);
  HttpParams httpParams = httpRequest.getParams();
  int timeoutMs = request.getTimeoutMs();
  // TODO: Reevaluate this connection timeout based on more wide-scale
  // data collection and possibly different for wifi vs. 3G.
  HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
  HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);
  return mClient.execute(httpRequest);
}

代码示例来源:origin: chentao0707/SimplifyReader

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
    throws IOException, AuthFailureError {
  String url = request.getUrl();
  HashMap<String, String> map = new HashMap<String, String>();
  map.putAll(request.getHeaders());
  map.putAll(additionalHeaders);
  if (mUrlRewriter != null) {

代码示例来源:origin: chentao0707/SimplifyReader

addCacheHeaders(headers, request.getCacheEntry());
  httpResponse = mHttpStack.performRequest(request, headers);
  StatusLine statusLine = httpResponse.getStatusLine();
    Entry entry = request.getCacheEntry();
    if (entry == null) {
      return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, null,
  attemptRetryOnException("connection", request, new TimeoutError());
} catch (MalformedURLException e) {
  throw new RuntimeException("Bad URL " + request.getUrl(), e);
} catch (IOException e) {
  int statusCode = 0;
    throw new NoConnectionError(e);
  VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
  if (responseContents != null) {
    networkResponse = new NetworkResponse(statusCode, responseContents,

代码示例来源:origin: chentao0707/SimplifyReader

@Override
  public boolean apply(Request<?> request) {
    return request.getTag() == tag;
  }
});

代码示例来源:origin: mcxiaoke/android-volley

@Override
public void deliverError(VolleyError error) {
  super.deliverError(error);
  deliverError_called = true;
}

代码示例来源:origin: mcxiaoke/android-volley

@Test public void cacheMiss() throws Exception {
  mCacheQueue.add(mRequest);
  mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
  assertFalse(mDelivery.wasEitherResponseCalled());
  assertTrue(mNetworkQueue.size() > 0);
  Request request = mNetworkQueue.take();
  assertNull(request.getCacheEntry());
}

代码示例来源:origin: mcxiaoke/android-volley

@Test public void cancelAll_onlyCorrectTag() throws Exception {
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 0, mDelivery);
    Object tagA = new Object();
    Object tagB = new Object();
    Request req1 = mock(Request.class);
    when(req1.getTag()).thenReturn(tagA);
    Request req2 = mock(Request.class);
    when(req2.getTag()).thenReturn(tagB);
    Request req3 = mock(Request.class);
    when(req3.getTag()).thenReturn(tagA);
    Request req4 = mock(Request.class);
    when(req4.getTag()).thenReturn(tagA);

    queue.add(req1); // A
    queue.add(req2); // B
    queue.add(req3); // A
    queue.cancelAll(tagA);
    queue.add(req4); // A

    verify(req1).cancel(); // A cancelled
    verify(req3).cancel(); // A cancelled
    verify(req2, never()).cancel(); // B not cancelled
    verify(req4, never()).cancel(); // A added after cancel not cancelled
  }
}

代码示例来源:origin: avluis/Hentoid

private static void setConnectionParametersForRequest(okhttp3.Request.Builder builder, Request<?> request)
    throws AuthFailureError {
  switch (request.getMethod()) {
    case Request.Method.DEPRECATED_GET_OR_POST:
      byte[] postBody = request.getBody();
      if (postBody != null) {
        builder.post(RequestBody.create(MediaType.parse(request.getBodyContentType()), postBody));

相关文章