废话不多说,先上两个工具类:用作普通的网络请求使用,文件的下载单独抽取一个工具类.
public class HttpURLBuild {
private final String TAG = "HttpURLBuild";
private String requestUrl = "";
private final String requestMethod;
final String PREFIX = "--"; //前缀
final String BOUNDARY = UUID.randomUUID().toString(); //边界标识
final String CONTENT_TYPE = "multipart/form-data"; //内容类型
final String LINE_END = "\r\n"; //换行
private static final String CHARSET = "utf-8"; //编码格式
// private HttpClientUtils.OnRequestCallBack callBackListener;
private HttpClientUtils.OnRequestCallBackBase callBackListenerBase;
public HttpURLBuild(String requestUrl, String requestMethod) {
this.requestUrl = requestUrl;
this.requestMethod = requestMethod;
}
HashMap<String, String> headMap = new HashMap<>();
HashMap<String, File> fileMap = new HashMap<>();
public HttpURLBuild setHead(String key, String value) {
headMap.put(key, value);
return this;
}
public HttpURLBuild addFile(String key, File file) {
fileMap.put(key, file);
return this;
}
Map<String, String> bodyMap = new HashMap<>();
String rawJson = "";
public HttpURLBuild addParameter(String key, String value) {
bodyMap.put(key, value);
return this;
}
public HttpURLBuild addParameter(Map<String, String> parameterMap) {
bodyMap.putAll(parameterMap);
return this;
}
public HttpURLBuild addRawData(String rawJson) {
this.rawJson = rawJson;
return this;
}
public HttpURLBuild setCallBack(HttpClientUtils.OnRequestCallBack callBackListener) {
if (callBackListener == null) {
return this;
}
this.callBackListenerBase = new HttpClientUtils.OnRequestCallBackBase(callBackListener);
return this;
}
StringBuilder builder = new StringBuilder();
//调用这个方法就开始正真的网络请求了 之前都是参数的设置,你别管我,让我看看
public HttpURLBuild build() {
if (isCancel) {
LogUtil.e(TAG, "请求已取消: " + requestUrl);
return null;
}
if (TextUtils.isEmpty(requestUrl)) {
throw new NullPointerException("请求地址不能为空");
}
if (callBackListenerBase == null) {
throw new IllegalArgumentException("回调地址不能为空,请检查setCallBack()是否调用");
}
AppExecutors.getInstance().networkIO().execute(new Runnable() {
@Override
public void run() {
// 现将参数写入缓冲区等待发送
DataOutputStream out = null;
boolean isSuccess = false;
String message;
InputStream inputStream = null;
ByteArrayOutputStream baos = null;
try {
if ("get".equalsIgnoreCase(requestMethod) && bodyMap.size() > 0) {
requestUrl += "?";
for (String key : bodyMap.keySet()) {
requestUrl += key;
requestUrl += bodyMap.get(key);
}
}
URL url = new URL(requestUrl);
final HttpURLConnection httpURLConnection;
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(60 * 1000);
// 设定请求的方法为"POST",默认是GET
httpURLConnection.setReadTimeout(60 * 1000);
if ("get".equalsIgnoreCase(requestMethod)) {
httpURLConnection.setRequestMethod("GET");
} else if ("post".equalsIgnoreCase(requestMethod)) {
httpURLConnection.setRequestMethod("POST");
// 设置是否向httpUrlConnection输出,如果是post请求,参数要放在http正文内,因此需要设为true, 默认是false;
httpURLConnection.setDoOutput(true);//这里设置成true就代表是post请求
}
/*
* 当我们要获取我们请求的http地址访问的数据时就是使用connection.getInputStream().read()方式时我们就需要setDoInput(true),
* 根据api文档我们可知doInput默认就是为true。我们可以不用手动设置了,如果不需要读取输入流的话那就setDoInput(false)。
* 当我们要采用非get请求给一个http网络地址传参 就是使用connection.getOutputStream().write() 方法时我们就需要setDoOutput(true), 默认是false
*/
// 设置是否从httpUrlConnection读入,默认情况下是true;
httpURLConnection.setDoInput(true);
//是否使用缓存
httpURLConnection.setUseCaches(false);
// httpURLConnection.setInstanceFollowRedirects(true); 刚加上的 瞎试的
httpURLConnection.setRequestProperty("accept", "*/*");//设置接收数据的格式
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");//设置连接方式为长连接
htt