该文章旨在翻译注释,理解每一个方法主要作什么,目的是练习本身的英文转译能力,为往后更顺畅的阅读源码做准备!java
该接口来源于Retrofit
源码,接口中定义了网络请求方法以及请求状态。web
package retrofit2; import java.io.IOException; import okhttp3.Request; /** * An invocation of a Retrofit method that sends a request to a webserver and returns a response. * Each call yields its own HTTP request and response pair. Use {@link #clone} to make multiple * calls with the same parameters to the same webserver; this may be used to implement polling or * to retry a failed call. * * <p>Calls may be executed synchronously with {@link #execute}, or asynchronously with {@link * #enqueue}. In either case the call can be canceled at any time with {@link #cancel}. A call that * is busy writing its request or reading its response may receive a {@link IOException}; this is * working as designed. * * @param <T> Successful response body type. */ public interface Call<T> extends Cloneable { /** * Synchronously send the request and return its response. * @throws IOException if a problem occurred talking to the server. * @throws RuntimeException (and subclasses) if an unexpected error occurs creating the request * or decoding the response. */ Response<T> execute() throws IOException; /** * Asynchronously send the request and notify {@code callback} of its response or if an error * occurred talking to the server, creating the request, or processing the response. */ void enqueue(Callback<T> callback); /** * Returns true if this call has been either {@linkplain #execute() executed} or {@linkplain * #enqueue(Callback) enqueued}. It is an error to execute or enqueue a call more than once. */ boolean isExecuted(); /** * Cancel this call. An attempt will be made to cancel in-flight calls, and if the call has not * yet been executed it never will be. */ void cancel(); /** True if {@link #cancel()} was called. */ boolean isCanceled(); /** * Create a new, identical call to this one which can be enqueued or executed even if this call * has already been. */ Call<T> clone(); /** The original HTTP request. */ Request request(); } 复制代码
一个retrofit方法的调用接口,经过这些方法能够向服务端发起请求并获得返回结果。 每个回调都会对应请求和返回结果。可经过execute
方法发起同步异步请求,或者 enqueue
发起异步请求,固然请求也可经过cancel
方法随时取消。当请求过于频繁时可能报IO异常。bash
<T>
表示 请求成功结果返回值类型markdown
/** * An invocation of a Retrofit method that sends a request to a webserver and returns a response. * Each call yields its own HTTP request and response pair. Use {@link #clone} to make multiple * calls with the same parameters to the same webserver; this may be used to implement polling or * to retry a failed call. * * <p>Calls may be executed synchronously with {@link #execute}, or asynchronously with {@link * #enqueue}. In either case the call can be canceled at any time with {@link #cancel}. A call that * is busy writing its request or reading its response may receive a {@link IOException}; this is * working as designed. * * @param <T> Successful response body type. */ 复制代码
/** * Synchronously send the request and return its response. * * @throws IOException if a problem occurred talking to the server. * @throws RuntimeException (and subclasses) if an unexpected error occurs creating the request * or decoding the response. */ Response<T> execute() throws IOException; 复制代码
/** * Asynchronously send the request and notify {@code callback} of its response or if an error * occurred talking to the server, creating the request, or processing the response. */ void enqueue(Callback<T> callback); 复制代码
/** * Returns true if this call has been either {@linkplain #execute() executed} or {@linkplain * #enqueue(Callback) enqueued}. It is an error to execute or enqueue a call more than once. */ boolean isExecuted(); 复制代码
/** * Cancel this call. An attempt will be made to cancel in-flight calls, and if the call has not * yet been executed it never will be. */ void cancel(); 复制代码
/** True if {@link #cancel()} was called. */ boolean isCanceled(); 复制代码
/** * Create a new, identical call to this one which can be enqueued or executed even if this call * has already been. */ Call<T> clone(); 复制代码
/** The original HTTP request. */
Request request();
复制代码