zoukankan      html  css  js  c++  java
  • Android网络开发之OkHttp--基本用法实例化各个对象

    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;
                }
            };
  • 相关阅读:
    【Object-C】判断指针类型,isKindOfxxx
    【Object-C】继承,super关键字
    Echart的angularjs封装
    ng-validate
    random background
    新发现。css3控制浏览器滚动条的样式
    如何灵活利用免费开源图标字体-IcoMoon篇
    干货分享:让你分分钟学会 javascript 闭包
    webpack
    css黑魔法
  • 原文地址:https://www.cnblogs.com/begin1949/p/4927717.html
Copyright © 2011-2022 走看看