1、封装前准备
1.1、定义一个请求方法的枚举类型
package com.gau.simplehttp.http;
public enum HttpMethod {
GET, POST
}
1.2、定义一个响应结果实体,用于接收服务器返回的数据
package com.gau.simplehttp.http;
public class Response {
private final int code;
private final String message;
private final String method;
private final String contentType;
private final int contentLength;
private final String body;
public Response(Builder builder) {
this.code = builder.code;
this.message = builder.message;
this.method = builder.method;
this.contentType = builder.contentType;
this.contentLength = builder.contentLength;
this.body = builder.body;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
public String getMethod() {
return method;
}
public String getContentType() {
return contentType;
}
public int getContentLength() {
return contentLength;
}
public boolean isSuccessful() {
return this.code >= 200 && this.code < 300;
}
public String getBody() {
return body;
}
public static class Builder {
private int code;
private String message;
private String method;
private String contentType;
private int contentLength;
private String body;
public Builder code(int code) {
this.code = code;
return this;
}
public Builder message(String message) {
this.message = message;
return this;
}
public Builder method(String method) {
this.method = method;
return this;
}
public Builder contentType(String contentType) {
this.contentType = contentType;
return this;
}
public Builder contentLength(int contentLength)