1添加依赖.
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.squareup.okhttp3:okhttp:3.4.1'
implementation 'com.squareup.retrofit2:retrofit:2.2.0'
implementation 'com.squareup.retrofit2:converter-gson:2.2.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.2.0'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.2.0'
2.RetrofitManager
package com.tsq.junbanpt.util.network;
import android.content.Context;
import android.support.test.espresso.core.internal.deps.guava.util.concurrent.ThreadFactoryBuilder;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tsq.junbanpt.MyApplication;
import com.tsq.junbanpt.R;
import com.tsq.junbanpt.interf.net.DeleteRequest;
import com.tsq.junbanpt.interf.net.FileRequest;
import com.tsq.junbanpt.interf.net.GetRequest;
import com.tsq.junbanpt.interf.net.PostRequest;
import com.tsq.junbanpt.interf.net.PutRequest;
import com.tsq.junbanpt.module.home.bean.AccessTokenBean;
import com.tsq.junbanpt.util.SharedPreferencesUtil;
import com.tsq.junbanpt.util.ToastUtil;
import com.tsq.junbanpt.util.bean.MessageEvent;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import static com.tsq.junbanpt.util.Constant.CHARACTER;
import static com.tsq.junbanpt.util.network.GetAccessToken.getNewToken;
/**
* @author xuwei
* on 2019/9/26
*/
public class RetrofitManager {
private static int TIME_OUT = 30;
private static HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
//打印retrofit日志
Log.e("RetrofitLog", "retrofitBack = " + message);
// if (TextUtils.isEmpty(message)) {
// return;
// }
// String s = message.substring(0, 1);
// if ("{".equals(s) || "[".equals(s)) {
// try {
// getToken(message);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
}
});
/**
* 获取token
*
* @param message
*/
private static void getToken(String message) throws Exception {
JSONObject jb;
String statusCode = "statusCode";
String code = "401";
jb = new JSONObject(message);
if (jb.optString(statusCode).equals(code)) {
String token = getNewToken();
SharedPreferencesUtil.put(MyApplication.getContext(), "token", token);
}
}
public static OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY))
.addInterceptor(new LoggingInterceptor())
.connectTimeout(TIME_OUT, TimeUnit.SECONDS)
.readTimeout(TIME_OUT, TimeUnit.SECONDS)
.writeTimeout(TIME_OUT, TimeUnit.SECONDS)
.build();
public static class LoggingInterceptor implements Interceptor {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
//第一步,获得chain内的request
Request request = chain.request();
Log.e("intercept", "intercept-request==:" + request.url());
//第二步,用chain执行request
okhttp3.Response response = chain.proceed(request);
Log.e("intercept", "intercept-response==" + "-" + response.request().url());
//第三步,返回response
if (response.code() == 401) {
String newSession = getNewToken();
//使用新的Token,创建新的请求
Request newRequest = chain.request()
.newBuilder()
.header("access_token", newSession)
.header("app_key", "1")
.build();
//重新请求
return chain.proceed(newRequest);
}
return response;
}
}
private static Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BaseUtil.HOST)
.client(client)
.build();
/**
* 发送GET网络请求
*
* @param url 请求地址
* @param clazz 返回的数据类型
* @param resultHandler 回调
* @param <T> 泛型
*/
public static <T> void sendGetRobot(String url, final Class<T> clazz, final AbstractResultHandler<T> resultHandler) {
// 判断网络连接状况
if (resultHandler.isNetDisconnected()) {
resultHandler.onAfterFailure();
return;
}
GetRequest getRequest = retrofit.create(GetRequest.class);
// 构建请求
Call<ResponseBody> call = getRequest.getUrl(addTokenMap(),url);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
resultHandler.onBeforeResult();
try {
ResponseBody body = response.body();
if (body == null) {
resultHandler.onServerError();
resultHandler.onAfterFailure();
return;
}
String string = body.string();
T t = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create().fromJson(string, clazz);
resultHandler.onResult(t);
} catch (IOException e) {
e.printStackTrace();
resultHandler.onFailure(e);
resultHandler.onAfterFailure();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
resultHandler.onFailure(t);
resultHandler.onAfterFailure();
}
});
}
/**
* 添加header
* @return
*/
public static Map addTokenMap() {
Map<String,String> map = new HashMap<String, String>();
map.put("access_token", (String) SharedPreferencesUtil.get(MyApplication.getContext(), "token", ""));
map.put("app_key","1");
return map;
}
/**
* 发送GET网络请求
*
* @param url 请求地址
* @param clazz 返回的数据类型
* @param resultHandler 回调
* @param <T> 泛型
*/
public static <T> void sendGetRequest(String url, final Class<T> clazz, final AbstractResultHandler<T> resultHandler) {
// 判断网络连接状况
if (resultHandler.isNetDisconnected()) {
resultHandler.onAfterFailure();
return;
}
GetRequest getRequest = retrofit.create(GetRequest.class);
// 构建请求
Call<ResponseBody> call = getRequest.getUrl(addTokenMap(),url);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
resultHandler.onBeforeResult();
try {
ResponseBody body = response.body();
if (body == null) {
resultHandler.onServerError();
resultHandler.onAfterFailure();
return;
}
String string = body.string();
string = string.replace("null(", "").replace(")", "");
T t = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create().fromJson(string, clazz);
resultHandler.onResult(t);
} catch (IOException e) {
e.printStackTrace();
resultHandler.onFailure(e);
resultHandler.onAfterFailure();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
resultHandler.onFailure(t);
resultHandler.onAfterFailure();
}
});
}
/**
* 发送Get网络请求 提交表单
*
* @param url 请求地址
* @param paramMap 参数列表
* @param clazz 返回的数据类型
* @param resultHandler 回调
* @param <T> 泛型
*/
public static <T> void sendGetRequestFrom(String url, Map<String, Object> paramMap, final Class<T> clazz, final AbstractResultHandler<T> resultHandler) {
// 判断网络连接状况
if (resultHandler.isNetDisconnected()) {
resultHandler.onAfterFailure();
return;
}
GetRequest getRequest = retrofit.create(GetRequest.class);
// 构建请求
Call<ResponseBody> call = getRequest.getForm(addTokenMap(),url, paramMap);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
resultHandler.onBeforeResult();
try {
ResponseBody body = response.body();
if (body == null) {
resultHandler.onServerError();
resultHandler.onAfterFailure();
return;
}
String string = body.string();
T t = new Gson().fromJson(string, clazz);
resultHandler.onResult(t);
} catch (IOException e) {
e.printStackTrace();
resultHandler.onFailure(e);
resultHandler.onAfterFailure();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
resultHandler.onFailure(t);
resultHandler.onAfterFailure();
}
});
}
/**
* 发送get网络请求 提交Json
*
* @param url 请求地址
* @param paramMap 参数列表
* @param clazz 返回的数据类型
* @param resultHandler 回调
* @param <T> 泛型
*/
public static <T> void sendGetRequestJson(String url, Map<String, Object> paramMap, final Class<T> clazz, final AbstractResultHandler<T> resultHandler) {
// 判断网络连接状况
if (resultHandler.isNetDisconnected()) {
resultHandler.onAfterFailure();
return;
}
GetRequest getRequest = retrofit.create(GetRequest.class);
// 构建请求
Call<ResponseBody> call = getRequest.getJson(addTokenMap(),url, getBody(paramMap));
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
resultHandler.onBeforeResult();
try {
ResponseBody body = response.body();
if (body == null) {
resultHandler.onServerError();
resultHandler.onAfterFailure();
return;
}
String string = body.string();
T t = new Gson().fromJson(string, clazz);
resultHandler.onResult(t);
} catch (IOException e) {
e.printStackTrace();
resultHandler.onFailure(e);
resultHandler.onAfterFailure();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
resultHandler.onFailure(t);
resultHandler.onAfterFailure();
}
});
}
/**
* 发送put网络请求
*
* @param url 请求地址
* @param clazz 返回的数据类型
* @param resultHandler 回调
* @param <T> 泛型
*/
public static <T> void sendPutRequest(String url, final Class<T> clazz, final AbstractResultHandler<T> resultHandler) {
// 判断网络连接状况
if (resultHandler.isNetDisconnected()) {
resultHandler.onAfterFailure();
return;
}
PutRequest putRequest = retrofit.create(PutRequest.class);
// 构建请求
Call<ResponseBody> call = putRequest.putUrl(addTokenMap(),url);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
resultHandler.onBeforeResult();
try {
ResponseBody body = response.body();
if (body == null) {
resultHandler.onServerError();
resultHandler.onAfterFailure();
return;
}
String string = body.string();
T t = new Gson().fromJson(string, clazz);
resultHandler.onResult(t);
} catch (IOException e) {
e.printStackTrace();
resultHandler.onFailure(e);
resultHandler.onAfterFailure();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
resultHandler.onFailure(t);
resultHandler.onAfterFailure();
}
});
}
/**
* 发送put网络请求 提交表单
*
* @param url 请求地址
* @param paramMap 参数列表
* @param clazz 返回的数据类型
* @param resultHandler 回调
* @param <T> 泛型
*/
public static <T> void sendPutRequestFrom(String url, Map<String, Object> paramMap, final Class<T> clazz, final AbstractResultHandler<T> resultHandler) {
// 判断网络连接状况
if (resultHandler.isNetDisconnected()) {
resultHandler.onAfterFailure();
return;
}
PutRequest putRequest = retrofit.create(PutRequest.class);
// 构建请求
Call<ResponseBody> call = putRequest.putForm(addTokenMap(),url, paramMap);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
resultHandler.onBeforeResult();
try {
ResponseBody body = response.body();
if (body == null) {
resultHandler.onServerError();
resultHandler.onAfterFailure();
return;
}
String string = body.string();
T t = new Gson().fromJson(string, clazz);
resultHandler.onResult(t);
} catch (IOException e) {
e.printStackTrace();
resultHandler.onFailure(e);
resultHandler.onAfterFailure();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
resultHandler.onFailure(t);
resultHandler.onAfterFailure();
}
});
}
/**
* 发送put网络请求 提交Json
*
* @param url 请求地址
* @param paramMap 参数列表
* @param clazz 返回的数据类型
* @param resultHandler 回调
* @param <T> 泛型
*/
public static <T> void sendPutRequestJson(String url, Map<String, Object> paramMap, final Class<T> clazz, final AbstractResultHandler<T> resultHandler) {
// 判断网络连接状况
if (resultHandler.isNetDisconnected()) {
resultHandler.onAfterFailure();
return;
}
PutRequest putRequest = retrofit.create(PutRequest.class);
// 构建请求
Call<ResponseBody> call = putRequest.putJson(addTokenMap(),url, getBody(paramMap));
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
resultHandler.onBeforeResult();
try {
if (response.code() == CHARACTER) {
ToastUtil.show(MyApplication.getContext(), "上传内容包含非法字符不允许上传");
return;
}
ResponseBody body = response.body();
if (body == null) {
resultHandler.onServerError();
resultHandler.onAfterFailure();
return;
}
String string = body.string();
T t = new Gson().fromJson(string, clazz);
resultHandler.onResult(t);
} catch (IOException e) {
e.printStackTrace();
resultHandler.onFailure(e);
resultHandler.onAfterFailure();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
resultHandler.onFailure(t);
resultHandler.onAfterFailure();
}
});
}
/**
* 发送post网络请求 提交表单
*
* @param url 请求地址
* @param paramMap 参数列表
* @param clazz 返回的数据类型
* @param resultHandler 回调
* @param <T> 泛型
*/
public static <T> void sendPostRequestFrom(String url, Map<String, Object> paramMap, final Class<T> clazz, final AbstractResultHandler<T> resultHandler) {
// 判断网络连接状况
if (resultHandler.isNetDisconnected()) {
resultHandler.onAfterFailure();
return;
}
PostRequest postRequest = retrofit.create(PostRequest.class);
// 构建请求
Call<ResponseBody> call = postRequest.postForm(addTokenMap(),url, paramMap);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
resultHandler.onBeforeResult();
try {
ResponseBody body = response.body();
if (body == null) {
resultHandler.onServerError();
resultHandler.onAfterFailure();
return;
}
String string = body.string();
T t = new Gson().fromJson(string, clazz);
resultHandler.onResult(t);
} catch (IOException e) {
e.printStackTrace();
resultHandler.onFailure(e);
resultHandler.onAfterFailure();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
resultHandler.onFailure(t);
resultHandler.onAfterFailure();
}
});
}
/**
* 发送post网络请求
*
* @param url 请求地址
* @param clazz 返回的数据类型
* @param resultHandler 回调
* @param <T> 泛型
*/
public static <T> void sendPostRequest(String url, final Class<T> clazz, final AbstractResultHandler<T> resultHandler) {
// 判断网络连接状况
if (resultHandler.isNetDisconnected()) {
resultHandler.onAfterFailure();
return;
}
PostRequest postRequest = retrofit.create(PostRequest.class);
// 构建请求
Call<ResponseBody> call = postRequest.postUrl(addTokenMap(),url);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
resultHandler.onBeforeResult();
try {
ResponseBody body = response.body();
if (body == null) {
resultHandler.onServerError();
resultHandler.onAfterFailure();
return;
}
String string = body.string();
T t = new Gson().fromJson(string, clazz);
resultHandler.onResult(t);
} catch (IOException e) {
e.printStackTrace();
resultHandler.onFailure(e);
resultHandler.onAfterFailure();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
resultHandler.onFailure(t);
resultHandler.onAfterFailure();
}
});
}
/**
* 发送post网络请求 提交Json
*
* @param url 请求地址
* @param paramMap 参数列表
* @param clazz 返回的数据类型
* @param resultHandler 回调
* @param <T> 泛型
*/
public static <T> void sendPostRequestJson(String url, Map<String, Object> paramMap, final Class<T> clazz, final AbstractResultHandler<T> resultHandler) {
// 判断网络连接状况
if (resultHandler.isNetDisconnected()) {
resultHandler.onAfterFailure();
return;
}
PostRequest postRequest = retrofit.create(PostRequest.class);
// 构建请求
Call<ResponseBody> call = postRequest.postJson(addTokenMap(),url, getBody(paramMap));
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
resultHandler.onBeforeResult();
try {
ResponseBody body = response.body();
if (body == null) {
resultHandler.onServerError();
resultHandler.onAfterFailure();
return;
}
String string = body.string();
T t = new Gson().fromJson(string, clazz);
resultHandler.onResult(t);
} catch (IOException e) {
e.printStackTrace();
resultHandler.onFailure(e);
resultHandler.onAfterFailure();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
resultHandler.onFailure(t);
resultHandler.onAfterFailure();
}
});
}
/**
* 发送post网络请求 提交二进制流
*
* @param url 请求地址
* @param paramMap 参数列表
* @param clazz 返回的数据类型
* @param resultHandler 回调
* @param <T> 泛型
* @param fileUtil Byte数组
*/
public static <T> void sendPostRequestStream(String url, Map<String, Object> paramMap, final Class<T> clazz, byte[] fileUtil, final AbstractResultHandler<T> resultHandler) {
// 判断网络连接状况
if (resultHandler.isNetDisconnected()) {
resultHandler.onAfterFailure();
return;
}
PostRequest postRequest = retrofit.create(PostRequest.class);
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), fileUtil);
// 构建请求
Call<ResponseBody> call = postRequest.postStream(addTokenMap(),url, paramMap, requestBody);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
resultHandler.onBeforeResult();
try {
ResponseBody body = response.body();
if (body == null) {
resultHandler.onServerError();
resultHandler.onAfterFailure();
return;
}
String string = body.string();
T t = new Gson().fromJson(string, clazz);
resultHandler.onResult(t);
} catch (IOException e) {
e.printStackTrace();
resultHandler.onFailure(e);
resultHandler.onAfterFailure();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
resultHandler.onFailure(t);
resultHandler.onAfterFailure();
}
});
}
/**
* 发送delete网络请求
*
* @param url 请求地址
* @param clazz 返回的数据类型
* @param resultHandler 回调
* @param <T> 泛型
*/
public static <T> void sendDeleteRequest(String url, final Class<T> clazz, final AbstractResultHandler<T> resultHandler) {
// 判断网络连接状况
if (resultHandler.isNetDisconnected()) {
resultHandler.onAfterFailure();
return;
}
DeleteRequest deleteRequest = retrofit.create(DeleteRequest.class);
// 构建请求
Call<ResponseBody> call = deleteRequest.deleteUrl(addTokenMap(),url);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
resultHandler.onBeforeResult();
try {
ResponseBody body = response.body();
if (body == null) {
resultHandler.onServerError();
resultHandler.onAfterFailure();
return;
}
String string = body.string();
T t = new Gson().fromJson(string, clazz);
resultHandler.onResult(t);
} catch (IOException e) {
e.printStackTrace();
resultHandler.onFailure(e);
resultHandler.onAfterFailure();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
resultHandler.onFailure(t);
resultHandler.onAfterFailure();
}
});
}
/**
* 发送delete网络请求 提交表单
*
* @param url 请求地址
* @param paramMap 参数列表
* @param clazz 返回的数据类型
* @param resultHandler 回调
* @param <T> 泛型
*/
public static <T> void sendDeleteRequestForm(String url, Map<String, Object> paramMap, final Class<T> clazz, final AbstractResultHandler<T> resultHandler) {
// 判断网络连接状况
if (resultHandler.isNetDisconnected()) {
resultHandler.onAfterFailure();
return;
}
DeleteRequest deleteRequest = retrofit.create(DeleteRequest.class);
// 构建请求
Call<ResponseBody> call = deleteRequest.deleteForm(addTokenMap(),url, paramMap);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
resultHandler.onBeforeResult();
try {
ResponseBody body = response.body();
if (body == null) {
resultHandler.onServerError();
resultHandler.onAfterFailure();
return;
}
String string = body.string();
T t = new Gson().fromJson(string, clazz);
resultHandler.onResult(t);
} catch (IOException e) {
e.printStackTrace();
resultHandler.onFailure(e);
resultHandler.onAfterFailure();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
resultHandler.onFailure(t);
resultHandler.onAfterFailure();
}
});
}
/**
* 发送delete网络请求 提交Json
*
* @param url 请求地址
* @param paramMap 参数列表
* @param clazz 返回的数据类型
* @param resultHandler 回调
* @param <T> 泛型
*/
public static <T> void sendDeleteRequestJson(String url, Map<String, String> paramMap, final Class<T> clazz, final AbstractResultHandler<T> resultHandler) {
// 判断网络连接状况
if (resultHandler.isNetDisconnected()) {
resultHandler.onAfterFailure();
return;
}
DeleteRequest deleteRequest = retrofit.create(DeleteRequest.class);
// 构建请求
Call<ResponseBody> call = deleteRequest.deleteJson(addTokenMap(),url, getBody(paramMap));
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
resultHandler.onBeforeResult();
try {
ResponseBody body = response.body();
if (body == null) {
resultHandler.onServerError();
resultHandler.onAfterFailure();
return;
}
String string = body.string();
T t = new Gson().fromJson(string, clazz);
resultHandler.onResult(t);
} catch (IOException e) {
e.printStackTrace();
resultHandler.onFailure(e);
resultHandler.onAfterFailure();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
resultHandler.onFailure(t);
resultHandler.onAfterFailure();
}
});
}
/**
* 发送上传文件网络请求
*
* @param url 请求地址
* @param clazz 返回的数据类型
* @param resultHandler 回调
* @param <T> 泛型
* @param file 上传的文件
*/
public static <T> void fileUpload(String url, File file, final Class<T> clazz, final AbstractResultHandler<T> resultHandler) {
// 判断网络连接状况
if (resultHandler.isNetDisconnected()) {
resultHandler.onAfterFailure();
return;
}
FileRequest fileRequest = retrofit.create(FileRequest.class);
Map<String, RequestBody> paramMap = new HashMap<>(16);
addMultiPart(paramMap, "file", file);
// 构建请求
Call<ResponseBody> call = fileRequest.postFile(addTokenMap(),url, paramMap);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
resultHandler.onBeforeResult();
try {
ResponseBody body = response.body();
if (body == null) {
resultHandler.onServerError();
resultHandler.onAfterFailure();
return;
}
String string = body.string();
T t = new Gson().fromJson(string, clazz);
resultHandler.onResult(t);
} catch (IOException e) {
e.printStackTrace();
resultHandler.onFailure(e);
resultHandler.onAfterFailure();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
resultHandler.onFailure(t);
resultHandler.onAfterFailure();
}
});
}
/**
* 文件下载
*
* @param url 下载地址
* @param downloadHandler 下载回调
*/
public static void fileDownload(String url, final DownloadHandler downloadHandler) {
// 回调方法执行器,定义回调在子线程中执行,避免Callback返回到MainThread,导致文件下载出现NetworkOnMainThreadException
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("demo-pool-%d").build();
ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
// 网络框架
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BaseUtil.HOST)
.callbackExecutor(singleThreadPool)
.build();
FileRequest fileRequest = retrofit.create(FileRequest.class);
// 构建请求
Call<ResponseBody> call = fileRequest.download(addTokenMap(),url);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
// 写入文件
downloadHandler.onBody(response.body());
} else {
downloadHandler.onError();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
downloadHandler.onError();
}
});
}
/**
* 添加多媒体类型
*/
private static void addMultiPart(Map<String, RequestBody> paramMap, String key, Object obj) {
if (obj instanceof String) {
RequestBody body = RequestBody.create(MediaType.parse("text/plain;charset=UTF-8"), (String) obj);
paramMap.put(key, body);
} else if (obj instanceof File) {
RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data;charset=UTF-8"), (File) obj);
paramMap.put(key + "\"; filename=\"" + ((File) obj).getName() + "", body);
}
}
/**
* 网络请求结果处理类
*
* @param <T> 请求结果封装对象
*/
public static abstract class AbstractResultHandler<T> {
Context context;
public AbstractResultHandler(Context context) {
this.context = context;
}
/**
* 判断网络是否未连接
*
* @return
*/
public boolean isNetDisconnected() {
return NetworkUtil.isNetDisconnected(context);
}
/**
* 请求成功之前
*/
public abstract void onBeforeResult();
/**
* 请求成功时
*
* @param t 结果数据
*/
public abstract void onResult(T t);
/**
* 服务器出错
*/
public void onServerError() {
// 服务器处理出错
// Toast.makeText(context, R.string.net_server_error, Toast.LENGTH_SHORT).show();
}
/**
* 请求失败后的处理
*/
public abstract void onAfterFailure();
/**
* 请求失败时的处理
*
* @param t
*/
public void onFailure(Throwable t) {
if (t instanceof SocketTimeoutException || t instanceof ConnectException) {
// 连接异常
if (NetworkUtil.isNetworkConnected(context)) {
// 服务器连接出错
Toast.makeText(context, R.string.net_server_connected_error, Toast.LENGTH_SHORT).show();
} else {
// 手机网络不通
Toast.makeText(context, R.string.net_not_connected, Toast.LENGTH_SHORT).show();
}
} else if (t instanceof Exception) {
// 功能异常
Toast.makeText(context, R.string.net_unknown_error, Toast.LENGTH_SHORT).show();
}
}
}
/**
* 文件下载回调
*/
public interface DownloadHandler {
/**
* 接收到数据体
*
* @param body 响应体
*/
public void onBody(ResponseBody body);
/**
* 文件下载出错
*/
public void onError();
}
/**
* map转换Json
*/
public static RequestBody getBody(Map map) {
Gson gson = new Gson();
String strEntity = gson.toJson(map);
Log.e("=====json串", strEntity);
RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
return body;
}
}
3.get请求
package com.tsq.junbanpt.interf.net;
import java.util.Map;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.DELETE;
import retrofit2.http.FieldMap;
import retrofit2.http.GET;
import retrofit2.http.HTTP;
import retrofit2.http.Header;
import retrofit2.http.HeaderMap;
import retrofit2.http.Headers;
import retrofit2.http.Url;
/**
* @author xuwei
* on 2019/9/26
* Get请求封装
*/
public interface GetRequest {
/**
* 发送Get请求请求
*
* @param url
* @return
*/
@GET
Call<ResponseBody> getUrl(@HeaderMap Map<String, String> headers, @Url String url);
/**
* 发送Get请求请求 表单
*
* @param url
* @param requestMap
* @return
*/
@GET
Call<ResponseBody> getForm(@HeaderMap Map<String, String> headers,@Url String url, @FieldMap Map<String, Object> requestMap);
/**
* 发送Get请求请求 json
*
* @param url
* @param route
* @return
*/
@HTTP(method = "GET", hasBody = true)
Call<ResponseBody> getJson(@HeaderMap Map<String, String> headers,@Url String url, @Body RequestBody route);
}
4 Delete请求
package com.tsq.junbanpt.interf.net;
import java.util.Map;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.DELETE;
import retrofit2.http.FieldMap;
import retrofit2.http.HeaderMap;
import retrofit2.http.Url;
/**
* @author xuwei
* on 2019/9/26
* delete请求封装
*/
public interface DeleteRequest {
/**
* 发送delete请求
* @param url
* @return
*/
@DELETE
Call<ResponseBody> deleteUrl(@HeaderMap Map<String, String> headers, @Url String url);
/**
* 发送delete请求 表单
* @param url
* @param requestMap
* @return
*/
@DELETE
Call<ResponseBody> deleteForm(@HeaderMap Map<String, String> headers,@Url String url, @FieldMap Map<String, Object> requestMap);
/**
* 发送delete请求 json
* @param url
* @param route
* @return
*/
@DELETE
Call<ResponseBody> deleteJson(@HeaderMap Map<String, String> headers,@Url String url, @Body RequestBody route);
}
5. FileRequest请求
package com.tsq.junbanpt.interf.net;
import java.util.Map;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.HeaderMap;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.PartMap;
import retrofit2.http.Streaming;
import retrofit2.http.Url;
/**
* @author xuwei
* on 2019/9/26
* 文件上传请求封装
*/
public interface FileRequest {
/**
* 上传文件请求
*
* @param url URL路径
* @param paramMap 请求参数
* @return
*/
@Multipart
@POST
Call<ResponseBody> postFile(@HeaderMap Map<String, String> headers, @Url String url, @PartMap Map<String, RequestBody> paramMap);
/**
* 下载文件get请求
*
* @param url 链接地址
* @return
*/
@Streaming
@GET
Call<ResponseBody> download(@HeaderMap Map<String, String> headers,@Url String url);
}
6. PostRequest 请求
package com.tsq.junbanpt.interf.net;
import com.tsq.junbanpt.util.FileUtil;
import java.util.Map;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.HeaderMap;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Part;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;
/**
* @author xuwei
* on 2019/9/26
* Post请求封装
*/
public interface PostRequest {
/**
* 发送Post请求
* @param url
* @return
*/
@POST
Call<ResponseBody> postUrl(@HeaderMap Map<String, String> headers,@Url String url);
/**
* 发送Post请求 表单
* @param url
* @param requestMap
* @return
*/
@FormUrlEncoded
@POST
Call<ResponseBody> postForm(@HeaderMap Map<String, String> headers,@Url String url, @FieldMap Map<String, Object> requestMap);
/**
* 发送Post请求 json
* @param url
* @param route
* @return
*/
@POST
Call<ResponseBody> postJson(@HeaderMap Map<String, String> headers, @Url String url, @Body RequestBody route);
/**
* 发送Post请求 二进制流
* @param url
* @param options
* @param file
* @return
*/
@Multipart
@POST
Call<ResponseBody> postStream(@HeaderMap Map<String, String> headers,@Url String url, @FieldMap Map<String, Object> options, @Part("file") RequestBody file);
}
7. PutRequest 请求
package com.tsq.junbanpt.interf.net;
import java.util.Map;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.HeaderMap;
import retrofit2.http.PUT;
import retrofit2.http.Url;
/**
* @author xuwei
* on 2019/9/26
* put请求封装
*/
public interface PutRequest {
/**
* 发送put请求
* @param url
* @return
*/
@PUT
Call<ResponseBody> putUrl(@HeaderMap Map<String, String> headers, @Url String url);
/**
* 发送put请求 表单
* @param url
* @param requestMap
* @return
*/
@FormUrlEncoded
@PUT
Call<ResponseBody> putForm(@HeaderMap Map<String, String> headers,@Url String url, @FieldMap Map<String, Object> requestMap);
/**
* 发送put请求 json
* @param url
* @param route
* @return
*/
@PUT
Call<ResponseBody> putJson(@HeaderMap Map<String, String> headers,@Url String url, @Body RequestBody route);
}
8.HttpResultArrayBean
public class HttpResultArrayBean<T> {
private int statusCode;
@SerializedName(value = "message", alternate = "msg")
private String msg;
private List<T> items;
public int getStatusCode() {
return statusCode;
}
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
public String getMsg() {
return msg == null ? "" : msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public List<T> getItems() {
if (items == null) {
return new ArrayList<>();
}
return items;
}
public void setItems(List<T> items) {
this.items = items;
}
}
9.HttpResultBean
public class HttpResultBean<T> {
private int statusCode;
@SerializedName(value = "message", alternate = "msg")
private String msg;
private T items;
public int getStatusCode() {
return statusCode;
}
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
public String getMsg() {
return msg == null ? "" : msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getItems() {
return items;
}
public void setItems(T items) {
this.items = items;
}
public HttpResultBean toResponse() {
HttpResultBean httpResultBean = new HttpResultBean();
httpResultBean.setStatusCode(statusCode);
httpResultBean.setMsg(msg);
return httpResultBean;
}
}
10.BaseUtil
public class BaseUtil {
public static final String HOST = "http://";}
11.应用
Map<String, Object> paramMap = new HashMap<>(16);
paramMap.put("appType", "0");
paramMap.put("appKey", "1");
RetrofitManager.sendPostRequestJson(BaseUtil.GETNEWVERSIONNUM, paramMap, VersionNumBean.class, new RetrofitManager.AbstractResultHandler<VersionNumBean>(MainActivity.this) {
@Override
public void onBeforeResult() {
}
@Override
public void onResult(VersionNumBean versionNumBean) {
}
@Override
public void onAfterFailure() {
}
});
12. VersionNumBean
/** * @author xuwei * on 2019/9/26 * 数据返回类型 */ public class ExampleResult { private int code; private String msg; /** * 数据部分也是一个bean,用JsonObject代替了 */ private JsonObject data; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public JsonObject getData() { return data; } public void setData(JsonObject data) { this.data = data; } }
// //缓存路径
// File cacheFile = new File(Environment.getExternalStorageDirectory(), "HttpCache");
// Cache cache = new Cache(cacheFile, 1024 * 1024 * 10);//缓存文件为100MB
// Log.e(TAG, "initRetrofit: " + cacheFile.getAbsolutePath());
//
// HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
// interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//
// OkHttpClient httpClient = new OkHttpClient.Builder()
// .addInterceptor(new Interceptor() {
// @Override
// public Response intercept(Chain chain) throws IOException {
// Request request = chain.request()
// .newBuilder()
.addHeader("TSQ-RequestId", BaseUtil.encryptByPublicKey())
// .build();
// return chain.proceed(request);
// }
// })
// .connectTimeout(5, TimeUnit.SECONDS)//设置连接超时
// .readTimeout(10, TimeUnit.SECONDS)//读取超时
// .writeTimeout(10, TimeUnit.SECONDS)//写入超时
// .addInterceptor(interceptor)//添加日志拦截器
.addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)//添加缓存拦截器
// .cache(cache)//把缓存添加进来
// .build();