美文网首页
Okhttp源码

Okhttp源码

作者: Y小圆脸 | 来源:发表于2017-04-17 15:43 被阅读14次

Protocol:协议,http 四种协议

Request:不可变的请求。包括url method headers body 。

在Request中有一个静态类Builder有属性url method headers body

构造方法

 public Builder() {
  this.method = "GET";
  this.headers = new Headers.Builder();//稍作解释
}

Builder(Request request) {
  this.url = request.url;
  this.method = request.method;
  this.body = request.body;
  this.tag = request.tag;
  this.headers = request.headers.newBuilder();
}

Builder静态类的方法:(把Builder作为返回类型)

 public Builder url(HttpUrl url) {}

 public Builder url(String url) {}

 public Builder url(URL url) {}
//替换头信息
 public Builder header(String name, String value) {}
//添加头信息
public Builder addHeader(String name, String value) {}
//移除头信息
public Builder removeHeader(String name) {}
//--------------------------------------
//请求方式
 public Builder get() {}
 public Builder post(RequestBody body) {}
 public Builder delete(RequestBody body) {}
 public Builder delete(){}
 public Builder put(RequestBody body) {}
 public Builder patch(RequestBody body) {}

public Builder method(String method, RequestBody body) {}
//返回Request对象
 public Request build() {}

Requst的方法:

//返回请求头
public Headers headers() {}
//获取头信息
public String header(String name) {}
//
public List<String> headers(String name) {}
//获取body
public RequestBody body() { return body;}
//新建Builder对象
public Builder newBuilder() {}

RequestBody:请求体是一个抽象类。

方法abstract contentType()//请求类型
public long contentLength() throws IOException {return -1;}//内容长度
public abstract void writeTo(BufferedSink sink) throws IOException;//写入请求内容

//返回请求body默认格式是"UTF-8"
`public static RequestBody create(MediaType contentType, String content) {}
//返回一个RequestBody请求体  
public static RequestBody create(final MediaType contentType, final ByteString content) {}
//返回一个RequestBody请求体,依据参数写入相应内容
public static RequestBody create(final MediaType contentType, final byte[] content,
  final int offset, final int byteCount) {}
//返回一个RequestBody请求体,将内容写入文件
 public static RequestBody create(final MediaType contentType, final File file) {}

Response响应

ResponseBody响应(抽象类)

`

Call:准备执行的调用(接口)

Request request();//请求

Response execute() throws IOException;//同步请求

void enqueue(Callback responseCallback);//异步请求

boolean isExecuted();//是否执行请求

boolean isCanceled();//是否取消请求

Call clone();//clone一个相同的请求

interface Factory {
Call newCall(Request request);}

WebSocket接口

四种状态:Connecting Open Closing Closed Canceled

Request request();//返回请求

long queueSize();//返回传输的信息长度    

boolean send(String text);

boolean send(ByteString bytes);

boolean close(int code, String reason);

void cancel();

interface Factory {
WebSocket newWebSocket(Request request, WebSocketListener listener);}

RealCall

implements Call接口

Callback:接口

onFailure连接失败 onResponse连接成功

Authenticator:接口

Request authenticate(Route route, Response response) throws IOException;//返回一个安全验证的接口

Headers:

头信息,由键值对组成,可以传入相应信息,有属性:String[] namesAndValues;Builder builder

构造方法

`Headers(Builder builder) {
this.namesAndValues = builder.namesAndValues.toArray(new String[builder.namesAndValues.size()]);
}

private Headers(String[] namesAndValues) {
this.namesAndValues = namesAndValues;
}`

方法

//从键值对的字符串中取出相应键的值。
  private static String get(String[] namesAndValues, String name) {
for (int i = namesAndValues.length - 2; i >= 0; i -= 2) {
  if (name.equalsIgnoreCase(namesAndValues[i])) {
    return namesAndValues[i + 1];
  }
}
return null;
}

调用方法:

public String get(String name){}//由键获取值

public Date getDate(String name) {}//获取日期

public int size() {}//键值对数量

public String name(int index) {}//由index返回键名称 

public String value(int index) {}//由index返回值名称

public Set<String> names() {}//一组键的集合

public List<String> values(String name) {}//一组对应键的值的集合

public Builder newBuilder() {}//创建一个新的Builder对象

//返回Headrs对象(通过键值对集合)
public static Headers of(Map<String, String> headers) {
if (headers == null) throw new NullPointerException("headers == null");

    // Make a defensive copy and clean it up.
    String[] namesAndValues = new String[headers.size() * 2];
    int i = 0;
    for (Map.Entry<String, String> header : headers.entrySet()) {
      if (header.getKey() == null || header.getValue() == null) {
        throw new IllegalArgumentException("Headers cannot be null");
      }
      String name = header.getKey().trim();
      String value = header.getValue().trim();
      if (name.length() == 0 || name.indexOf('\0') != -1 || value.indexOf('\0') != -1) {
        throw new IllegalArgumentException("Unexpected header: " + name + ": " + value);
      }
      namesAndValues[i] = name;
      namesAndValues[i + 1] = value;
      i += 2;
    }

    return new Headers(namesAndValues); }

Builder静态内部类:

//向头信息添加一组键值对
 public Builder add(String name, String value) {
  checkNameAndValue(name, value);//检查合法性
  return addLenient(name, value);
}
 //添加键值对信息
 Builder addLenient(String name, String value) {
  namesAndValues.add(name);
  namesAndValues.add(value.trim());
  return this;
}

//由键名称移除相应键值。
public Builder removeAll(String name) {
  for (int i = 0; i < namesAndValues.size(); i += 2) {
    if (name.equalsIgnoreCase(namesAndValues.get(i))) {
      namesAndValues.remove(i); // name
      namesAndValues.remove(i); // value
      i -= 2;
    }
  }
  return this;
}

//替换相应键值对。
 public Builder set(String name, String value) {
  checkNameAndValue(name, value);
  removeAll(name);
  addLenient(name, value);
  return this;
}

//由键获取相应的值。
 public String get(String name) {
  for (int i = namesAndValues.size() - 2; i >= 0; i -= 2) {
    if (name.equalsIgnoreCase(namesAndValues.get(i))) {
      return namesAndValues.get(i + 1);
    }
  }
  return null;
}

//返回相应的Header对象。
public Headers build() {
  return new Headers(this);
}

Connection接口

Route route();//返回连接路由

Socket socket();//返回使用的socket

Handshake handshake();//返回建立连接的握手

Protocol protocol();//返回使用协议

ConnectionPool连接池

ConnectionSpec连接规范

Route

相关文章

网友评论

      本文标题:Okhttp源码

      本文链接:https://www.haomeiwen.com/subject/lwmtzttx.html