com.ejlchina.http.internal.RealHttpResult Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of httputils Show documentation
Show all versions of httputils Show documentation
Http工具包,封装 OkHttp,链式用法、支持异步同步、响应JSON自动解析,回调线程切换、配置灵活、支持URL占位符、支持BaseUrl、外部依赖少、适用 JavaWeb 和 Android,教程全面、简单易用
package com.ejlchina.http.internal;
import java.util.List;
import java.util.concurrent.Executor;
import com.ejlchina.http.HttpResult;
import okhttp3.Headers;
import okhttp3.Response;
public class RealHttpResult implements HttpResult {
private State state;
private Response response;
private Exception error;
private Executor callbackExecutor;
public RealHttpResult() {
}
public RealHttpResult(State state) {
this.state = state;
}
public RealHttpResult(Response response, Executor callbackExecutor) {
this(callbackExecutor);
response(response);
}
public RealHttpResult(Executor callbackExecutor) {
this.callbackExecutor = callbackExecutor;
}
public RealHttpResult(State state, Exception error) {
exception(state, error);
}
public void exception(State state, Exception error) {
this.state = state;
this.error = error;
}
public void response(Response response) {
this.state = State.RESPONSED;
this.response = response;
}
@Override
public State getState() {
return state;
}
@Override
public int getStatus() {
if (response != null) {
return response.code();
}
return 0;
}
@Override
public boolean isSuccessful() {
if (response != null) {
return response.isSuccessful();
}
return false;
}
@Override
public Headers getHeaders() {
if (response != null) {
return response.headers();
}
return null;
}
@Override
public List getHeaders(String name) {
return response.headers(name);
}
@Override
public String getHeader(String name) {
return response.header(name);
}
@Override
public Body getBody() {
if (response != null) {
return new ResultBody(response, callbackExecutor);
}
return null;
}
@Override
public Exception getError() {
return error;
}
public Response getResponse() {
return response;
}
@Override
public String toString() {
Body body = getBody();
String str = "HttpResult [\n state: " + state + ",\n status: " + getStatus()
+ ",\n headers: " + getHeaders();
if (body != null) {
str += ",\n contentType: " + body.getContentType()
+ ",\n body: " + body.toString();
}
return str + ",\n error: " + error + "\n]";
}
@Override
public HttpResult close() {
if (response != null) {
response.close();
}
return this;
}
}