1、实例化OkHttpClient对象,OkHttpClient包含了以下属性,以及set()和get()方法。但并没有包含具体的执行方法,详情见源码。
//实例化OkHttpClent对象 private OkHttpClient client = new OkHttpClient();
private static SSLSocketFactory defaultSslSocketFactory; private final RouteDatabase routeDatabase; private Dispatcher dispatcher; private Proxy proxy; private List<Protocol> protocols; private List<ConnectionSpec> connectionSpecs; private final List<Interceptor> interceptors = new ArrayList<>(); private final List<Interceptor> networkInterceptors = new ArrayList<>(); private ProxySelector proxySelector; private CookieHandler cookieHandler; /** Non-null if this client is caching; possibly by {@code cache}. */ private InternalCache internalCache; private Cache cache; private SocketFactory socketFactory; private SSLSocketFactory sslSocketFactory; private HostnameVerifier hostnameVerifier; private CertificatePinner certificatePinner; private Authenticator authenticator; private ConnectionPool connectionPool; private Network network; private boolean followSslRedirects = true; private boolean followRedirects = true; private boolean retryOnConnectionFailure = true; private int connectTimeout = 10_000; private int readTimeout = 10_000; private int writeTimeout = 10_000;
2、实例化Request对象,大家可以参考Request源码。
(1)、大家可以先查考--java builder模式 http://www.cnblogs.com/moonz-wu/archive/2011/01/11/1932473.html
(2)、Request、Response即采用了Builder模式。
(3)、Request包含了HttpUrl、Method、Headers、RequestBody、Tag等参数。以及httpUrl()、method()、headers()、body()、tag()等方法(类似于get()方法)。
(4)、并没有包含有关网络操作和其他操作的方法。
3、实例化RequestBody方法,大家可以参考RequestBody源码。
(1)、第一可以通过RequestBody内部静态方法实现。
RequestBody body = RequestBody.create(final MediaType contentType, final ByteString content);
(2)、第二可以通过重写RequestBody的writeTo()和contentType()方法。
RequestBody body = new RequestBody() { @Override public void writeTo(BufferedSink arg0) throws IOException { // TODO Auto-generated method stub } @Override public MediaType contentType() { // TODO Auto-generated method stub return null; } };