Protocol:協(xié)議,http 四種協(xié)議
Request:不可變的請求。包括url method headers body 。
在Request中有一個靜態(tài)類Builder有屬性url method headers body
構(gòu)造方法:
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靜態(tài)類的方法:(把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;}//內(nèi)容長度
public abstract void writeTo(BufferedSink sink) throws IOException;//寫入請求內(nèi)容
//返回請求body默認格式是"UTF-8"
`public static RequestBody create(MediaType contentType, String content) {}
//返回一個RequestBody請求體
public static RequestBody create(final MediaType contentType, final ByteString content) {}
//返回一個RequestBody請求體,依據(jù)參數(shù)寫入相應(yīng)內(nèi)容
public static RequestBody create(final MediaType contentType, final byte[] content,
final int offset, final int byteCount) {}
//返回一個RequestBody請求體,將內(nèi)容寫入文件
public static RequestBody create(final MediaType contentType, final File file) {}
Response響應(yīng)
ResponseBody響應(yīng)(抽象類)
`
Call:準(zhǔn)備執(zhí)行的調(diào)用(接口)
Request request();//請求
Response execute() throws IOException;//同步請求
void enqueue(Callback responseCallback);//異步請求
boolean isExecuted();//是否執(zhí)行請求
boolean isCanceled();//是否取消請求
Call clone();//clone一個相同的請求
interface Factory {
Call newCall(Request request);}
WebSocket接口
四種狀態(tài):Connecting Open Closing Closed Canceled
Request request();//返回請求
long queueSize();//返回傳輸?shù)男畔㈤L度
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:
頭信息,由鍵值對組成,可以傳入相應(yīng)信息,有屬性:String[] namesAndValues;和Builder builder
構(gòu)造方法
`Headers(Builder builder) {
this.namesAndValues = builder.namesAndValues.toArray(new String[builder.namesAndValues.size()]);
}
private Headers(String[] namesAndValues) {
this.namesAndValues = namesAndValues;
}`
方法
//從鍵值對的字符串中取出相應(yīng)鍵的值。
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;
}
調(diào)用方法:
public String get(String name){}//由鍵獲取值
public Date getDate(String name) {}//獲取日期
public int size() {}//鍵值對數(shù)量
public String name(int index) {}//由index返回鍵名稱
public String value(int index) {}//由index返回值名稱
public Set<String> names() {}//一組鍵的集合
public List<String> values(String name) {}//一組對應(yīng)鍵的值的集合
public Builder newBuilder() {}//創(chuàng)建一個新的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靜態(tài)內(nèi)部類:
//向頭信息添加一組鍵值對
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;
}
//由鍵名稱移除相應(yīng)鍵值。
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;
}
//替換相應(yīng)鍵值對。
public Builder set(String name, String value) {
checkNameAndValue(name, value);
removeAll(name);
addLenient(name, value);
return this;
}
//由鍵獲取相應(yīng)的值。
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;
}
//返回相應(yīng)的Header對象。
public Headers build() {
return new Headers(this);
}
Connection接口
Route route();//返回連接路由
Socket socket();//返回使用的socket
Handshake handshake();//返回建立連接的握手
Protocol protocol();//返回使用協(xié)議