Android網絡開發之OkHttp--基本用法實例化各個對象

一、實例化OkHttpClient對象,OkHttpClient包含了如下屬性,以及set()和get()方法。但並無包含具體的執行方法,詳情見源碼。html

  //實例化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;

二、實例化Request對象,你們能夠參考Request源碼。java

    (1)、你們能夠先查考--java builder模式 http://www.cnblogs.com/moonz-wu/archive/2011/01/11/1932473.htmlcookie

    (2)、Request、Response即採用了Builder模式。網絡

    (3)、Request包含了HttpUrl、Method、Headers、RequestBody、Tag等參數。以及httpUrl()、method()、headers()、body()、tag()等方法(相似於get()方法)。socket

    (4)、並無包含有關網絡操做和其餘操做的方法。ide

三、實例化RequestBody方法,你們能夠參考RequestBody源碼。ui

    (1)、第一能夠經過RequestBody內部靜態方法實現。this

RequestBody body = RequestBody.create(final MediaType contentType, final ByteString content);

    (2)、第二能夠經過重寫RequestBody的writeTo()和contentType()方法。spa

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;
            }
        };
相關文章
相關標籤/搜索