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

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

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

Call.cancel介绍

[英]Cancels the request, if possible. Requests that are already complete cannot be canceled.
[中]如果可能,取消请求。无法取消已完成的请求。

代码示例

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

  1. public void cancel() {
  2. canceled = true;
  3. okhttp3.Call call;
  4. synchronized (this) {
  5. call = rawCall;
  6. }
  7. if (call != null) {
  8. call.cancel();
  9. }
  10. }

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

  1. @Override
  2. protected void interruptTask() {
  3. this.call.cancel();
  4. }
  5. }

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

  1. @Override public void cancel() {
  2. call.cancel();
  3. }

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

  1. @Override public void cancel() {
  2. call.cancel();
  3. }

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

  1. @Override
  2. public void cancel() {
  3. Call local = call;
  4. if (local != null) {
  5. local.cancel();
  6. }
  7. }

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

  1. @Override
  2. public void cancel() {
  3. canceled = true;
  4. if (rawCall != null) {
  5. rawCall.cancel();
  6. }
  7. }

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

  1. @Override
  2. public void cancel() {
  3. Call local = call;
  4. if (local != null) {
  5. local.cancel();
  6. }
  7. }

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

  1. @Override
  2. protected void interruptTask() {
  3. this.call.cancel();
  4. }
  5. }

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

  1. @Override
  2. public void dispose() {
  3. this.call.cancel();
  4. }

代码示例来源:origin: com.squareup.retrofit2/retrofit

  1. public void cancel() {
  2. canceled = true;
  3. okhttp3.Call call;
  4. synchronized (this) {
  5. call = rawCall;
  6. }
  7. if (call != null) {
  8. call.cancel();
  9. }
  10. }

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

  1. @Override public void disconnect() {
  2. // Calling disconnect() before a connection exists should have no effect.
  3. if (call == null) return;
  4. networkInterceptor.proceed(); // Unblock any waiting async thread.
  5. call.cancel();
  6. }

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

  1. public void run() throws Exception {
  2. Request request = new Request.Builder()
  3. .url("http://httpbin.org/delay/2") // This URL is served with a 2 second delay.
  4. .build();
  5. final long startNanos = System.nanoTime();
  6. final Call call = client.newCall(request);
  7. // Schedule a job to cancel the call in 1 second.
  8. executor.schedule(() -> {
  9. System.out.printf("%.2f Canceling call.%n", (System.nanoTime() - startNanos) / 1e9f);
  10. call.cancel();
  11. System.out.printf("%.2f Canceled call.%n", (System.nanoTime() - startNanos) / 1e9f);
  12. }, 1, TimeUnit.SECONDS);
  13. System.out.printf("%.2f Executing call.%n", (System.nanoTime() - startNanos) / 1e9f);
  14. try (Response response = call.execute()) {
  15. System.out.printf("%.2f Call was expected to fail, but completed: %s%n",
  16. (System.nanoTime() - startNanos) / 1e9f, response);
  17. } catch (IOException e) {
  18. System.out.printf("%.2f Call failed as expected: %s%n",
  19. (System.nanoTime() - startNanos) / 1e9f, e);
  20. }
  21. }

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

  1. @Override public Response<T> execute() throws IOException {
  2. okhttp3.Call call;
  3. synchronized (this) {
  4. if (executed) throw new IllegalStateException("Already executed.");
  5. executed = true;
  6. if (creationFailure != null) {
  7. if (creationFailure instanceof IOException) {
  8. throw (IOException) creationFailure;
  9. } else if (creationFailure instanceof RuntimeException) {
  10. throw (RuntimeException) creationFailure;
  11. } else {
  12. throw (Error) creationFailure;
  13. }
  14. }
  15. call = rawCall;
  16. if (call == null) {
  17. try {
  18. call = rawCall = createRawCall();
  19. } catch (IOException | RuntimeException | Error e) {
  20. throwIfFatal(e); // Do not assign a fatal error to creationFailure.
  21. creationFailure = e;
  22. throw e;
  23. }
  24. }
  25. }
  26. if (canceled) {
  27. call.cancel();
  28. }
  29. return parseResponse(call.execute());
  30. }

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

  1. @Override
  2. public synchronized okhttp3.Call prepareRawCall() throws Throwable {
  3. if (executed) throw HttpException.COMMON("Already executed!");
  4. executed = true;
  5. rawCall = request.getRawCall();
  6. if (canceled) rawCall.cancel();
  7. return rawCall;
  8. }

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

  1. @Override
  2. public void uploadProgress(Progress innerProgress) {
  3. if (rawCall.isCanceled()) return;
  4. if (progress.status != Progress.LOADING) {
  5. rawCall.cancel();
  6. return;
  7. }
  8. progress.from(innerProgress);
  9. postLoading(progress);
  10. }
  11. });

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

  1. /** 取消所有请求请求 */
  2. public static void cancelAll(OkHttpClient client) {
  3. if (client == null) return;
  4. for (Call call : client.dispatcher().queuedCalls()) {
  5. call.cancel();
  6. }
  7. for (Call call : client.dispatcher().runningCalls()) {
  8. call.cancel();
  9. }
  10. }
  11. }

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

  1. @Override
  2. public void unsubscribe() {
  3. if (request.getCall() != null) {
  4. request.getCall().cancel();
  5. }
  6. }

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

  1. /** 取消所有请求请求 */
  2. public void cancelAll() {
  3. for (Call call : getOkHttpClient().dispatcher().queuedCalls()) {
  4. call.cancel();
  5. }
  6. for (Call call : getOkHttpClient().dispatcher().runningCalls()) {
  7. call.cancel();
  8. }
  9. }

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

  1. /** 根据Tag取消请求 */
  2. public static void cancelTag(OkHttpClient client, Object tag) {
  3. if (client == null || tag == null) return;
  4. for (Call call : client.dispatcher().queuedCalls()) {
  5. if (tag.equals(call.request().tag())) {
  6. call.cancel();
  7. }
  8. }
  9. for (Call call : client.dispatcher().runningCalls()) {
  10. if (tag.equals(call.request().tag())) {
  11. call.cancel();
  12. }
  13. }
  14. }

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

  1. /** 根据Tag取消请求 */
  2. public void cancelTag(Object tag) {
  3. if (tag == null) return;
  4. for (Call call : getOkHttpClient().dispatcher().queuedCalls()) {
  5. if (tag.equals(call.request().tag())) {
  6. call.cancel();
  7. }
  8. }
  9. for (Call call : getOkHttpClient().dispatcher().runningCalls()) {
  10. if (tag.equals(call.request().tag())) {
  11. call.cancel();
  12. }
  13. }
  14. }

相关文章