okhttp3.Call.enqueue()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(8.4k)|赞(0)|评价(0)|浏览(862)

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

Call.enqueue介绍

[英]Schedules the request to be executed at some point in the future.

The OkHttpClient#getDispatcher defines when the request will run: usually immediately unless there are several other requests currently being executed.

This client will later call back responseCallback with either an HTTP response or a failure exception. If you #cancel a request before it completes the callback will not be invoked.
[中]计划在将来的某个时间执行请求。
OkHttpClient#getDispatcher定义了请求何时运行:通常是立即运行,除非当前正在执行多个其他请求。
此客户端稍后将使用HTTP响应或故障异常回调responseCallback。如果在请求完成之前取消请求,则不会调用回调。

代码示例

代码示例来源:origin: spring-projects/spring-framework

public OkHttpListenableFuture(Call call) {
  this.call = call;
  this.call.enqueue(new Callback() {
    @Override
    public void onResponse(Call call, Response response) {
      set(new OkHttp3ClientHttpResponse(response));
    }
    @Override
    public void onFailure(Call call, IOException ex) {
      setException(ex);
    }
  });
}

代码示例来源:origin: org.springframework/spring-web

public OkHttpListenableFuture(Call call) {
  this.call = call;
  this.call.enqueue(new Callback() {
    @Override
    public void onResponse(Call call, Response response) {
      set(new OkHttp3ClientHttpResponse(response));
    }
    @Override
    public void onFailure(Call call, IOException ex) {
      setException(ex);
    }
  });
}

代码示例来源:origin: googlemaps/google-maps-services-java

@Override
public void setCallback(Callback<T> callback) {
 this.callback = callback;
 call.enqueue(this);
}

代码示例来源:origin: square/okhttp

public void run() throws Exception {
 Request request = new Request.Builder()
   .url("http://publicobject.com/helloworld.txt")
   .build();
 client.newCall(request).enqueue(new Callback() {
  @Override public void onFailure(Call call, IOException e) {
   e.printStackTrace();
  }
  @Override public void onResponse(Call call, Response response) throws IOException {
   try (ResponseBody responseBody = response.body()) {
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
    Headers responseHeaders = response.headers();
    for (int i = 0, size = responseHeaders.size(); i < size; i++) {
     System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
    }
    System.out.println(responseBody.string());
   }
  }
 });
}

代码示例来源:origin: square/okhttp

private void executeRequests(final String hostname, List<Call> networkRequests,
  final List<InetAddress> responses, final List<Exception> failures) {
 final CountDownLatch latch = new CountDownLatch(networkRequests.size());
 for (Call call : networkRequests) {
  call.enqueue(new Callback() {
   @Override public void onFailure(Call call, IOException e) {
    synchronized (failures) {
     failures.add(e);
    }
    latch.countDown();
   }
   @Override public void onResponse(Call call, Response response) {
    processResponse(response, hostname, responses, failures);
    latch.countDown();
   }
  });
 }
 try {
  latch.await();
 } catch (InterruptedException e) {
  failures.add(e);
 }
}

代码示例来源:origin: square/okhttp

public void run() throws Exception {
 Request washingtonPostRequest = new Request.Builder()
   .url("https://www.washingtonpost.com/")
   .build();
 client.newCall(washingtonPostRequest).enqueue(new Callback() {
  @Override public void onFailure(Call call, IOException e) {
  }
  @Override public void onResponse(Call call, Response response) throws IOException {
   try (ResponseBody body = response.body()) {
    // Consume and discard the response body.
    body.source().readByteString();
   }
  }
 });
 Request newYorkTimesRequest = new Request.Builder()
   .url("https://www.nytimes.com/")
   .build();
 client.newCall(newYorkTimesRequest).enqueue(new Callback() {
  @Override public void onFailure(Call call, IOException e) {
  }
  @Override public void onResponse(Call call, Response response) throws IOException {
   try (ResponseBody body = response.body()) {
    // Consume and discard the response body.
    body.source().readByteString();
   }
  }
 });
}

代码示例来源:origin: stackoverflow.com

public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();

Call post(String url, String json, Callback callback) {
 RequestBody body = RequestBody.create(JSON, json);
 Request request = new Request.Builder()
   .url(url)
   .post(body)
   .build();
 Call call = client.newCall(request);
 call.enqueue(callback);
 return call;
}

代码示例来源:origin: square/okhttp

public void connect(OkHttpClient client) {
 client = client.newBuilder()
   .eventListener(EventListener.NONE)
   .build();
 call = client.newCall(request);
 call.timeout().clearTimeout();
 call.enqueue(this);
}

代码示例来源:origin: square/okhttp

@Override public void connect() throws IOException {
 if (executed) return;
 Call call = buildCall();
 executed = true;
 call.enqueue(this);
 synchronized (lock) {
  try {
   while (connectPending && response == null && callFailure == null) {
    lock.wait(); // Wait 'til the network interceptor is reached or the call fails.
   }
   if (callFailure != null) {
    throw propagate(callFailure);
   }
  } catch (InterruptedException e) {
   Thread.currentThread().interrupt(); // Retain interrupted status.
   throw new InterruptedIOException();
  }
 }
}

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

@Override
public void loadData(@NonNull Priority priority,
  @NonNull final DataCallback<? super InputStream> callback) {
 Request.Builder requestBuilder = new Request.Builder().url(url.toStringUrl());
 for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
  String key = headerEntry.getKey();
  requestBuilder.addHeader(key, headerEntry.getValue());
 }
 Request request = requestBuilder.build();
 this.callback = callback;
 call = client.newCall(request);
 call.enqueue(this);
}

代码示例来源:origin: guolindev/booksource

public static void sendOkHttpRequest(String address, okhttp3.Callback callback) {
  OkHttpClient client = new OkHttpClient();
  Request request = new Request.Builder().url(address).build();
  client.newCall(request).enqueue(callback);
}

代码示例来源:origin: apache/flink

public void send(DatadogHttpReporter.DatadogHttpRequest request) throws Exception {
  String postBody = serialize(request.getSeries());
  Request r = new Request.Builder()
    .url(seriesUrl)
    .post(RequestBody.create(MEDIA_TYPE, postBody))
    .build();
  client.newCall(r).enqueue(EmptyCallback.getEmptyCallback());
}

代码示例来源:origin: JessYanCoding/MVPArms

@Override
public void loadData(@NonNull Priority priority,
           @NonNull final DataCallback<? super InputStream> callback) {
  Request.Builder requestBuilder = new Request.Builder().url(url.toStringUrl());
  for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
    String key = headerEntry.getKey();
    requestBuilder.addHeader(key, headerEntry.getValue());
  }
  Request request = requestBuilder.build();
  this.callback = callback;
  call = client.newCall(request);
  call.enqueue(this);
}

代码示例来源:origin: square/okhttp

call = Internal.instance.newWebSocketCall(client, request);
call.timeout().clearTimeout();
call.enqueue(new Callback() {
 @Override public void onResponse(Call call, Response response) {
  StreamAllocation streamAllocation = Internal.instance.streamAllocation(call);

代码示例来源:origin: prestodb/presto

private void httpDelete(URI uri)
{
  Request request = prepareRequest(HttpUrl.get(uri))
      .delete()
      .build();
  httpClient.newCall(request).enqueue(new NullCallback());
}

代码示例来源:origin: square/retrofit

call.enqueue(new okhttp3.Callback() {
 @Override public void onResponse(okhttp3.Call call, okhttp3.Response rawResponse) {
  Response<T> response;

代码示例来源:origin: hidroh/materialistic

@Test
public void testVoteNoMatchingAccount() {
  Preferences.setUsername(RuntimeEnvironment.application, "another");
  UserServices.Callback callback = mock(UserServices.Callback.class);
  assertFalse(userServices.voteUp(RuntimeEnvironment.application, "1", callback));
  verify(call, never()).enqueue(any(Callback.class));
}

代码示例来源:origin: hidroh/materialistic

@Test
public void testCommentNotLoggedIn() {
  Preferences.setUsername(RuntimeEnvironment.application, null);
  UserServices.Callback callback = mock(UserServices.Callback.class);
  userServices.reply(RuntimeEnvironment.application, "1", "reply", callback);
  verify(call, never()).enqueue(any(Callback.class));
  verify(callback).onDone(eq(false));
}

代码示例来源:origin: hidroh/materialistic

@Test
public void testVoteNoAccount() {
  ShadowAccountManager.get(RuntimeEnvironment.application)
      .removeAccount(account, null, null);
  UserServices.Callback callback = mock(UserServices.Callback.class);
  assertFalse(userServices.voteUp(RuntimeEnvironment.application, "1", callback));
  verify(call, never()).enqueue(any(Callback.class));
}

代码示例来源:origin: jeasonlzy/okhttp-OkGo

@Override
public void onFailure(okhttp3.Call call, IOException e) {
  if (e instanceof SocketTimeoutException && currentRetryCount < request.getRetryCount()) {
    //retry when timeout
    currentRetryCount++;
    rawCall = request.getRawCall();
    if (canceled) {
      rawCall.cancel();
    } else {
      rawCall.enqueue(this);
    }
  } else {
    if (!call.isCanceled()) {
      Response<T> error = Response.error(false, call, null, e);
      onError(error);
    }
  }
}

相关文章