public class HttpClient {
private final String URL;
private final WeakHashMap<String, String> PARAMS;
private final IRequest REQUEST;
private final ISuccess SUCCESS;
private final IFailure FAILURE;
private final IError ERROR;
private final IMistake MISTAKE;
private final RequestBody BODY;
private final Context CONTEXT;
public HttpClient(String url, IRequest iRequest, ISuccess iSuccess, IFailure iFailure, IError iError, IMistake iMistake, RequestBody body, WeakHashMap<String, String> params, Context context) {
this.URL = url;
this.REQUEST = iRequest;
this.SUCCESS = iSuccess;
this.FAILURE = iFailure;
this.ERROR = iError;
this.MISTAKE = iMistake;
this.BODY = body;
this.PARAMS = params;
this.CONTEXT = context;
}
public static HttpClientBuilder builder() {
return new HttpClientBuilder();
}
public void request(HttpMethod httpMethod) {
OkHttpClient client = HttpCreator.getOkHttpClient();
Call call = null;
if (REQUEST != null) {
REQUEST.onRequestStart();
}
switch (httpMethod) {
case GET:
call = get(client);
break;
case POST:
call = post(client);
break;
case POST_RAW:
call = postRaw(client);
break;
}
if (call != null) {
call.enqueue(getRequestCallback());
}
}
private Call get(OkHttpClient client) {
String url = URL;
boolean first = true;
for (Map.Entry<String, String> entry : PARAMS.entrySet()) {
url += first ? "?" : "&";
url += entry.getKey() + "=" + entry.getValue();
first = false;
}
Request request = new Request.Builder()
.get()
.url(url)
.build();
return client.newCall(request);
}
private Call post(OkHttpClient client) {
FormBody.Builder builder = new FormBody.Builder();
for (Map.Entry<String, String> entry : PARAMS.entrySet()) {
builder.add(entry.getKey(), entry.getValue());
}
RequestBody requestBodyPost = builder.build();
Request request = new Request.Builder()
.url(URL)
.post(requestBodyPost)
.build();
return client.newCall(request);
}
private Call postRaw(OkHttpClient client) {
Request request = new Request.Builder()
.url(URL)
.post(BODY)
.build();
return client.newCall(request);
}
private Callback getRequestCallback() {
return new RequestCallbacks(
REQUEST,
SUCCESS,
FAILURE,
ERROR,
MISTAKE
);
}
public void get() {
request(HttpMethod.GET);
}
public void post() {
if (BODY != null) {
request(HttpMethod.POST_RAW);
} else {
request(HttpMethod.POST);
}
}
}
/*************************************************************/
public class HttpClientBuilder {
private String mUrl;
private WeakHashMap<String, String> mParams = new WeakHashMap<>();
private IRequest mRequest;
private ISuccess mSuccess;
private IFailure mFailure;
private IError mError;
private IMistake mMistake;
private RequestBody mBody;
private Context mContext;
public HttpClientBuilder url(String url) {
this.mUrl = url;
return this;
}
public HttpClientBuilder params(WeakHashMap<String, String> params) {
this.mParams.putAll(params);
return this;
}
public HttpClientBuilder raw(String raw) {
this.mBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), raw);
return this;
}
public HttpClientBuilder onRequest(IRequest iRequest) {
this.mRequest = iRequest;
return this;
}
public HttpClientBuilder onSuccess(ISuccess iSuccess) {
this.mSuccess = iSuccess;
return this;
}
public HttpClientBuilder onFailure(IFailure iFailure) {
this.mFailure = iFailure;
return this;
}
public HttpClientBuilder onError(IError iError) {
this.mError = iError;
return this;
}
public HttpClientBuilder onMistake(IMistake iMistake) {
this.mMistake = iMistake;
return this;
}
public final HttpClient build() {
return new HttpClient(mUrl, mRequest, mSuccess, mFailure, mError, mMistake, mBody, mParams, mContext);
}
}
/*************************************************************/
public class HttpCreator {
private static final class OkHttpHolder {
private static final int TIME_OUT = 60;
private static final OkHttpClient.Builder BUILDER = new OkHttpClient.Builder();
// TODO: 2018/4/11 这里可以添加拦截器
private static final OkHttpClient.Builder addInterceptors() {
// 日志拦截器(只在debug模式下输出)
if (BuildConfig.DEBUG) {
BUILDER.addInterceptor(new LogInterceptor());
}
return BUILDER;
}
private static final OkHttpClient OK_HTTP_CLIENT = addInterceptors()
.connectTimeout(TIME_OUT, TimeUnit.SECONDS)
.build();
}
public static OkHttpClient getOkHttpClient() {
return OkHttpHolder.OK_HTTP_CLIENT;
}
}
/*************************************************************/
public enum HttpMethod {
GET, // get请求
POST, // 表单请求
POST_RAW, // json请求
}
/*************************************************************/
public abstract class GsonSuccess<T> implements ISuccess {
protected Class<T> mClzBean;
public GsonSuccess() {
ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
mClzBean = (Class<T>) pt.getActualTypeArguments()[0];
}
@Override
public void onSuccess(String onResponse) {
if (!TextUtils.isEmpty(onResponse)) {
onSuccess((T) GsonUtil.fromJson(onResponse, mClzBean));
} else {
onSuccess((T) null);
}
}
public abstract void onSuccess(T t);
}
/*************************************************************/
public class RequestCallbacks implements Callback {
private final IRequest REQUEST;
private final ISuccess SUCCESS;
private final IFailure FAILURE;
private final IError ERROR;
private final IMistake MISTAKE;
public RequestCallbacks(IRequest request, ISuccess success, IFailure failure, IError error, IMistake mistake) {
this.REQUEST = request;
this.SUCCESS = success;
this.FAILURE = failure;
this.ERROR = error;
this.MISTAKE = mistake;
}
@Override
public void onFailure(Call call, IOException e) {
if (FAILURE != null) {
FAILURE.onFailure(e.getMessage());
}
if (MISTAKE != null) {
MISTAKE.onMistake(e.getMessage());
}
callRequestEnd();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
if (call.isExecuted()) {
if (SUCCESS != null) {
SUCCESS.onSuccess(response.body().string());
}
}
} else {
if (ERROR != null) {
ERROR.onError(response.code(), response.message());
}
if (MISTAKE != null) {
MISTAKE.onMistake(response.message());
}
}
callRequestEnd();
}
private void callRequestEnd() {
if (REQUEST != null) {
REQUEST.onRequestEnd();
}
}
}
网友评论