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

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

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

Call.enqueue介绍

[英]Asynchronously send the request and notify callback of its response or if an error occurred talking to the server, creating the request, or processing the response.
[中]异步发送请求并通知回调其响应,或者在与服务器交谈、创建请求或处理响应时发生错误。

代码示例

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

@Override public ListenableFuture<Response<R>> adapt(final Call<R> call) {
  return new AbstractFuture<Response<R>>() {
   {
    call.enqueue(new Callback<R>() {
     @Override public void onResponse(Call<R> call, Response<R> response) {
      set(response);
     }
     @Override public void onFailure(Call<R> call, Throwable t) {
      setException(t);
     }
    });
   }
   @Override protected void interruptTask() {
    call.cancel();
   }
  };
 }
}

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

@Override public CompletableFuture<Response<R>> adapt(final Call<R> call) {
  final CompletableFuture<Response<R>> future = new CompletableFuture<Response<R>>() {
   @Override public boolean cancel(boolean mayInterruptIfRunning) {
    if (mayInterruptIfRunning) {
     call.cancel();
    }
    return super.cancel(mayInterruptIfRunning);
   }
  };
  call.enqueue(new Callback<R>() {
   @Override public void onResponse(Call<R> call, Response<R> response) {
    future.complete(response);
   }
   @Override public void onFailure(Call<R> call, Throwable t) {
    future.completeExceptionally(t);
   }
  });
  return future;
 }
}

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

@Override public CompletableFuture<Response<R>> adapt(final Call<R> call) {
  final CompletableFuture<Response<R>> future = new CompletableFuture<Response<R>>() {
   @Override public boolean cancel(boolean mayInterruptIfRunning) {
    if (mayInterruptIfRunning) {
     call.cancel();
    }
    return super.cancel(mayInterruptIfRunning);
   }
  };
  call.enqueue(new Callback<R>() {
   @Override public void onResponse(Call<R> call, Response<R> response) {
    future.complete(response);
   }
   @Override public void onFailure(Call<R> call, Throwable t) {
    future.completeExceptionally(t);
   }
  });
  return future;
 }
}

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

@Override public ListenableFuture<R> adapt(final Call<R> call) {
  return new AbstractFuture<R>() {
   {
    call.enqueue(new Callback<R>() {
     @Override public void onResponse(Call<R> call, Response<R> response) {
      if (response.isSuccessful()) {
       set(response.body());
      } else {
       setException(new HttpException(response));
      }
     }
     @Override public void onFailure(Call<R> call, Throwable t) {
      setException(t);
     }
    });
   }
   @Override protected void interruptTask() {
    call.cancel();
   }
  };
 }
}

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

@Override public CompletableFuture<R> adapt(final Call<R> call) {
  final CompletableFuture<R> future = new CompletableFuture<R>() {
   @Override public boolean cancel(boolean mayInterruptIfRunning) {
    if (mayInterruptIfRunning) {
     call.cancel();
    }
    return super.cancel(mayInterruptIfRunning);
   }
  };
  call.enqueue(new Callback<R>() {
   @Override public void onResponse(Call<R> call, Response<R> response) {
    if (response.isSuccessful()) {
     future.complete(response.body());
    } else {
     future.completeExceptionally(new HttpException(response));
    }
   }
   @Override public void onFailure(Call<R> call, Throwable t) {
    future.completeExceptionally(t);
   }
  });
  return future;
 }
}

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

@Override public CompletableFuture<R> adapt(final Call<R> call) {
  final CompletableFuture<R> future = new CompletableFuture<R>() {
   @Override public boolean cancel(boolean mayInterruptIfRunning) {
    if (mayInterruptIfRunning) {
     call.cancel();
    }
    return super.cancel(mayInterruptIfRunning);
   }
  };
  call.enqueue(new Callback<R>() {
   @Override public void onResponse(Call<R> call, Response<R> response) {
    if (response.isSuccessful()) {
     future.complete(response.body());
    } else {
     future.completeExceptionally(new HttpException(response));
    }
   }
   @Override public void onFailure(Call<R> call, Throwable t) {
    future.completeExceptionally(t);
   }
  });
  return future;
 }
}

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

@Override public void enqueue(final MyCallback<T> callback) {
 call.enqueue(new Callback<T>() {
  @Override public void onResponse(Call<T> call, Response<T> response) {
   // TODO if 'callbackExecutor' is not null, the 'callback' methods should be executed
   // on that executor by submitting a Runnable. This is left as an exercise for the reader.
   int code = response.code();
   if (code >= 200 && code < 300) {
    callback.success(response);
   } else if (code == 401) {
    callback.unauthenticated(response);
   } else if (code >= 400 && code < 500) {
    callback.clientError(response);
   } else if (code >= 500 && code < 600) {
    callback.serverError(response);
   } else {
    callback.unexpectedError(new RuntimeException("Unexpected response " + response));
   }
  }
  @Override public void onFailure(Call<T> call, Throwable t) {
   // TODO if 'callbackExecutor' is not null, the 'callback' methods should be executed
   // on that executor by submitting a Runnable. This is left as an exercise for the reader.
   if (t instanceof IOException) {
    callback.networkError((IOException) t);
   } else {
    callback.unexpectedError(t);
   }
  }
 });
}

代码示例来源:origin: resilience4j/resilience4j

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

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

@Override public void enqueue(Callback<T> callback) {
 getDelegate().enqueue(callback);
}

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

@Override public void enqueue(final Callback<T> callback) {
 checkNotNull(callback, "callback == null");
 delegate.enqueue(new Callback<T>() {
  @Override public void onResponse(Call<T> call, final Response<T> response) {
   callbackExecutor.execute(new Runnable() {
    @Override public void run() {
     if (delegate.isCanceled()) {
      // Emulate OkHttp's behavior of throwing/delivering an IOException on cancellation.
      callback.onFailure(ExecutorCallbackCall.this, new IOException("Canceled"));
     } else {
      callback.onResponse(ExecutorCallbackCall.this, response);
     }
    }
   });
  }
  @Override public void onFailure(Call<T> call, final Throwable t) {
   callbackExecutor.execute(new Runnable() {
    @Override public void run() {
     callback.onFailure(ExecutorCallbackCall.this, t);
    }
   });
  }
 });
}

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

@Override public Future<Response<T>> adapt(Call<T> call) {
  Promise<Response<T>> promise = Promise.apply();

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

   @Override public void onFailure(Call<T> call, Throwable t) {
    promise.failure(t);
   }
  });

  return promise.future();
 }
}

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

public void crawlPage(HttpUrl url) {
 // Skip hosts that we've visited many times.
 AtomicInteger hostnameCount = new AtomicInteger();
 AtomicInteger previous = hostnames.putIfAbsent(url.host(), hostnameCount);
 if (previous != null) hostnameCount = previous;
 if (hostnameCount.incrementAndGet() > 100) return;
 // Asynchronously visit URL.
 pageService.get(url).enqueue(new Callback<Page>() {
  @Override public void onResponse(Call<Page> call, Response<Page> response) {
   if (!response.isSuccessful()) {
    System.out.println(call.request().url() + ": failed: " + response.code());
    return;
   }
   // Print this page's URL and title.
   Page page = response.body();
   HttpUrl base = response.raw().request().url();
   System.out.println(base + ": " + page.title);
   // Enqueue its links for visiting.
   for (String link : page.links) {
    HttpUrl linkUrl = base.resolve(link);
    if (linkUrl != null && fetchedUrls.add(linkUrl)) {
     crawlPage(linkUrl);
    }
   }
  }
  @Override public void onFailure(Call<Page> call, Throwable t) {
   System.out.println(call.request().url() + ": failed: " + t);
  }
 });
}

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

@Override public Future<T> adapt(Call<T> call) {
  Promise<T> promise = Promise.apply();

  call.enqueue(new Callback<T>() {
   @Override public void onResponse(Call<T> call, Response<T> response) {
    if (response.isSuccessful()) {
     promise.success(response.body());
    } else {
     promise.failure(new HttpException(response));
    }
   }

   @Override public void onFailure(Call<T> call, Throwable t) {
    promise.failure(t);
   }
  });

  return promise.future();
 }
}

代码示例来源:origin: commonsguy/cw-omnibus

private void loadQuestions() {
 so.questions("android").enqueue(this);
}

代码示例来源:origin: commonsguy/cw-omnibus

@Override
public void onViewCreated(@NonNull View view,
             @Nullable Bundle savedInstanceState) {
 super.onViewCreated(view, savedInstanceState);
 Retrofit retrofit=
  new Retrofit.Builder()
   .baseUrl("https://api.stackexchange.com")
   .addConverterFactory(GsonConverterFactory.create())
   .build();
 StackOverflowInterface so=
  retrofit.create(StackOverflowInterface.class);
 so.questions("android").enqueue(this);
}

代码示例来源:origin: commonsguy/cw-omnibus

@Override
public void onViewCreated(@NonNull View view,
             @Nullable Bundle savedInstanceState) {
 super.onViewCreated(view, savedInstanceState);
 Retrofit retrofit=
  new Retrofit.Builder()
   .baseUrl("https://api.stackexchange.com")
   .addConverterFactory(GsonConverterFactory.create())
   .build();
 StackOverflowInterface so=
  retrofit.create(StackOverflowInterface.class);
 so.questions("android").enqueue(this);
}

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

@Override protected void subscribeActual(Observer<? super Response<T>> observer) {
 // Since Call is a one-shot type, clone it for each new observer.
 Call<T> call = originalCall.clone();
 CallCallback<T> callback = new CallCallback<>(call, observer);
 observer.onSubscribe(callback);
 if (!callback.isDisposed()) {
  call.enqueue(callback);
 }
}

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

@Override public void call(Subscriber<? super Response<T>> subscriber) {
  // Since Call is a one-shot type, clone it for each new subscriber.
  Call<T> call = originalCall.clone();
  final CallArbiter<T> arbiter = new CallArbiter<>(call, subscriber);
  subscriber.add(arbiter);
  subscriber.setProducer(arbiter);

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

   @Override public void onFailure(Call<T> call, Throwable t) {
    Exceptions.throwIfFatal(t);
    arbiter.emitError(t);
   }
  });
 }
}

代码示例来源:origin: resilience4j/resilience4j

@Override
public void enqueue(final Callback<T> callback) {
  try {
    RateLimiter.waitForPermission(rateLimiter);
  } catch (RequestNotPermitted | IllegalStateException e) {
    callback.onResponse(call, tooManyRequestsError());
    return;
  }
  call.enqueue(callback);
}

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

@Override public void run() {
  if (canceled) {
   callback.onFailure(BehaviorCall.this, new IOException("canceled"));
  } else if (behavior.calculateIsFailure()) {
   if (delaySleep()) {
    callback.onFailure(BehaviorCall.this, behavior.failureException());
   }
  } else if (behavior.calculateIsError()) {
   if (delaySleep()) {
    //noinspection unchecked An error response has no body.
    callback.onResponse(BehaviorCall.this, (Response<T>) behavior.createErrorResponse());
   }
  } else {
   delegate.enqueue(new Callback<T>() {
    @Override public void onResponse(Call<T> call, Response<T> response) {
     if (delaySleep()) {
      callback.onResponse(call, response);
     }
    }
    @Override public void onFailure(Call<T> call, Throwable t) {
     if (delaySleep()) {
      callback.onFailure(call, t);
     }
    }
   });
  }
 }
});

相关文章