All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ejlchina.http.internal.RealHttpResult Maven / Gradle / Ivy

Go to download

Http工具包,封装 OkHttp,链式用法、支持异步同步、响应JSON自动解析,回调线程切换、配置灵活、支持URL占位符、支持BaseUrl、外部依赖少、适用 JavaWeb 和 Android,教程全面、简单易用

There is a newer version: 2.3.0
Show newest version
package com.ejlchina.http.internal;

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;
	
	public RealHttpResult() {
	}
	
	public RealHttpResult(State state) {
		this.state = state;
	}
	
	public RealHttpResult(Response response) {
		response(response);
	}
	
	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 Body getBody() {
		if (response != null) {
			return new ResultBody(response.body());
		}
		return null;
	}
	
	@Override
	public Exception getError() {
		return error;
	}

	@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]";
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy