public class OkGoUtils {
/**
* 必須在Application中初始化
* @param context Application對象
* @author LH
* created at 2019/9/25 10:23
*/
public static void init(Application context){
OkHttpClient.Builder builder = new OkHttpClient.Builder();
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY); //log打印級別,決定了log顯示的詳細程度
loggingInterceptor.setColorLevel(Level.INFO); //log顏色級別,決定了log在控制檯顯示的顏色
builder.addInterceptor(loggingInterceptor); //添加OkGo默認debug日誌
builder.readTimeout(10000, TimeUnit.MILLISECONDS); //全局的讀取超時時間
builder.writeTimeout(10000, TimeUnit.MILLISECONDS); //全局的寫入超時時間
builder.connectTimeout(10000, TimeUnit.MILLISECONDS); //全局的連接超時時間
OkGo.getInstance().init(context) //必須調用初始化
.setOkHttpClient(builder.build()) //建議設置OkHttpClient,不設置會使用默認的
.setCacheMode(CacheMode.NO_CACHE) //全局統一緩存模式,默認不使用緩存,可以不傳
.setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE) //全局統一緩存時間,默認永不過期,可以不傳
.setRetryCount(0);
}
/**
* get請求
* @param context 當前對象
* @param url 請求地址
* @param bodyValue body鍵值對
* @param onRequestExecute 請求完成回調
* @author LH
* created at 2019/9/20 10:48
*/
public static void httpGetRequest(final Context context, final String url, HashMap<String,Object> bodyValue, final OnRequestExecute onRequestExecute){
try {
LogUtils.Logs_e("http請求地址:"+url);
GetRequest<String> params = OkGo.<String>get(url).tag(context.getApplicationContext());
if(bodyValue != null && bodyValue.size() != 0) {
LogUtils.Logs_e("body爲:"+bodyValue.toString());
Set<Map.Entry<String, Object>> bodySet = bodyValue.entrySet();
for (Map.Entry<String, Object> entry : bodySet) {
Object objectValue = entry.getValue();
if (objectValue instanceof String) {
params.params(entry.getKey(), String.valueOf(objectValue));
} else if (objectValue instanceof Integer) {
params.params(entry.getKey(), Integer.parseInt(String.valueOf(objectValue)));
} else if (objectValue instanceof Float) {
params.params(entry.getKey(), Float.parseFloat(String.valueOf(objectValue)));
} else if (objectValue instanceof Double) {
params.params(entry.getKey(), Double.parseDouble(String.valueOf(objectValue)));
} else if (objectValue instanceof Long) {
params.params(entry.getKey(), Long.parseLong(String.valueOf(objectValue)));
} else if (objectValue instanceof Character) {
params.params(entry.getKey(), (Character) objectValue);
} else if (objectValue instanceof Boolean) {
params.params(entry.getKey(), Boolean.parseBoolean(String.valueOf(objectValue)));
} else {
LogUtils.Logs_e("body中有不支持的類型");
}
}
}
params.execute(new StringCallback() {
@Override
public void onStart(Request<String, ? extends Request> request) {
super.onStart(request);
if(onRequestExecute != null){
onRequestExecute.onStart();
}
}
@Override
public void onSuccess(Response<String> response) {
LogUtils.Logs_e("接口"+url.substring(url.lastIndexOf("/"))+"請求結果:"+response.body());
if(onRequestExecute != null){
onRequestExecute.onSuccess(response);
}
}
@Override
public void onError(Response<String> response) {
super.onError(response);
LogUtils.Logs_e("地址:"+url+"請求失敗");
if(response != null && response.getException() != null) {
LogUtils.Logs_e("原因:"+response.getException().toString());
}
if(onRequestExecute != null){
onRequestExecute.onError(response);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* get請求
* @param context 當前對象
* @param url 請求地址
* @param headValue head鍵值對
* @param bodyValue body鍵值對
* @param onRequestExecute 請求完成回調
* @author LH
* created at 2019/9/20 10:48
*/
public static void httpGetRequest(final Context context, final String url, HashMap<String,Object> headValue, HashMap<String,Object> bodyValue, final OnRequestExecute onRequestExecute){
try {
LogUtils.Logs_e("http請求地址:"+url);
GetRequest<String> params = OkGo.<String>get(url).tag(context.getApplicationContext());
if(headValue != null && headValue.size() != 0) {
LogUtils.Logs_e("header爲:"+headValue.toString());
Set<Map.Entry<String, Object>> headSet = headValue.entrySet();
HttpHeaders headers = new HttpHeaders();
for (Map.Entry<String, Object> entry : headSet) {
Object objectValue = entry.getValue();
if (objectValue instanceof String) {
headers.put(entry.getKey(), String.valueOf(objectValue));
} else {
LogUtils.Logs_e("header中有不支持的類型");
}
}
params.headers(headers);
}
if(bodyValue != null && bodyValue.size() != 0) {
LogUtils.Logs_e("body爲:"+bodyValue.toString());
Set<Map.Entry<String, Object>> bodySet = bodyValue.entrySet();
for (Map.Entry<String, Object> entry : bodySet) {
Object objectValue = entry.getValue();
if (objectValue instanceof String) {
params.params(entry.getKey(), String.valueOf(objectValue));
} else if (objectValue instanceof Integer) {
params.params(entry.getKey(), Integer.parseInt(String.valueOf(objectValue)));
} else if (objectValue instanceof Float) {
params.params(entry.getKey(), Float.parseFloat(String.valueOf(objectValue)));
} else if (objectValue instanceof Double) {
params.params(entry.getKey(), Double.parseDouble(String.valueOf(objectValue)));
} else if (objectValue instanceof Long) {
params.params(entry.getKey(), Long.parseLong(String.valueOf(objectValue)));
} else if (objectValue instanceof Character) {
params.params(entry.getKey(), (Character) objectValue);
} else if (objectValue instanceof Boolean) {
params.params(entry.getKey(), Boolean.parseBoolean(String.valueOf(objectValue)));
} else {
LogUtils.Logs_e("body中有不支持的類型");
}
}
}
params.execute(new StringCallback() {
@Override
public void onStart(Request<String, ? extends Request> request) {
super.onStart(request);
if(onRequestExecute != null){
onRequestExecute.onStart();
}
}
@Override
public void onSuccess(Response<String> response) {
LogUtils.Logs_e("接口"+url.substring(url.lastIndexOf("/"))+"請求結果:"+response.body());
if(onRequestExecute != null){
onRequestExecute.onSuccess(response);
}
}
@Override
public void onError(Response<String> response) {
super.onError(response);
LogUtils.Logs_e("地址:"+url+"請求失敗");
if(response != null && response.getException() != null) {
LogUtils.Logs_e("原因:"+response.getException().toString());
}
if(onRequestExecute != null){
onRequestExecute.onError(response);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* post請求
* @param context 當前對象
* @param url 請求地址
* @param bodyValue body鍵值對
* @param onRequestExecute 請求完成回調
* @author LH
* created at 2019/9/20 10:48
*/
public static void httpPostRequest(final Context context, final String url, HashMap<String,Object> bodyValue, final OnRequestExecute onRequestExecute){
try {
LogUtils.Logs_e("http請求地址:"+url);
PostRequest<String> params = OkGo.<String>post(url).tag(context.getApplicationContext());
if(bodyValue != null && bodyValue.size() != 0) {
LogUtils.Logs_e("body爲:"+bodyValue.toString());
Set<Map.Entry<String, Object>> bodySet = bodyValue.entrySet();
for (Map.Entry<String, Object> entry : bodySet) {
Object objectValue = entry.getValue();
if (objectValue instanceof String) {
params.params(entry.getKey(), String.valueOf(objectValue));
} else if (objectValue instanceof Integer) {
params.params(entry.getKey(), Integer.parseInt(String.valueOf(objectValue)));
} else if (objectValue instanceof Float) {
params.params(entry.getKey(), Float.parseFloat(String.valueOf(objectValue)));
} else if (objectValue instanceof Double) {
params.params(entry.getKey(), Double.parseDouble(String.valueOf(objectValue)));
} else if (objectValue instanceof Long) {
params.params(entry.getKey(), Long.parseLong(String.valueOf(objectValue)));
} else if (objectValue instanceof Character) {
params.params(entry.getKey(), (Character) objectValue);
} else if (objectValue instanceof Boolean) {
params.params(entry.getKey(), Boolean.parseBoolean(String.valueOf(objectValue)));
} else {
LogUtils.Logs_e("body中有不支持的類型");
}
}
}
params.execute(new StringCallback() {
@Override
public void onStart(Request<String, ? extends Request> request) {
super.onStart(request);
if(onRequestExecute != null){
onRequestExecute.onStart();
}
}
@Override
public void onSuccess(Response<String> response) {
LogUtils.Logs_e("接口"+url.substring(url.lastIndexOf("/"))+"請求結果:"+response.body());
if(onRequestExecute != null){
onRequestExecute.onSuccess(response);
}
}
@Override
public void onError(Response<String> response) {
super.onError(response);
LogUtils.Logs_e("地址:"+url+"請求失敗");
if(response != null && response.getException() != null) {
LogUtils.Logs_e("原因:"+response.getException().toString());
}
if(onRequestExecute != null){
onRequestExecute.onError(response);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* post請求
* @param context 當前對象
* @param url 請求地址
* @param headValue head鍵值對
* @param bodyValue body鍵值對
* @param onRequestExecute 請求完成回調
* @author LH
* created at 2019/9/20 10:48
*/
public static void httpPostRequest(final Context context, final String url, HashMap<String,Object> headValue, HashMap<String,Object> bodyValue, final OnRequestExecute onRequestExecute){
try {
LogUtils.Logs_e("http請求地址:"+url);
PostRequest<String> params = OkGo.<String>post(url).tag(context.getApplicationContext());
if(headValue != null && headValue.size() != 0) {
LogUtils.Logs_e("header爲:"+headValue.toString());
HttpHeaders headers = new HttpHeaders();
Set<Map.Entry<String, Object>> headSet = headValue.entrySet();
for (Map.Entry<String, Object> entry : headSet) {
Object objectValue = entry.getValue();
if (objectValue instanceof String) {
headers.put(entry.getKey(), String.valueOf(objectValue));
} else {
LogUtils.Logs_e("header中有不支持的類型");
}
}
params.headers(headers);
}
if(bodyValue != null && bodyValue.size() != 0) {
LogUtils.Logs_e("body爲:"+bodyValue.toString());
Set<Map.Entry<String, Object>> bodySet = bodyValue.entrySet();
for (Map.Entry<String, Object> entry : bodySet) {
Object objectValue = entry.getValue();
if (objectValue instanceof String) {
params.params(entry.getKey(), String.valueOf(objectValue));
} else if (objectValue instanceof Integer) {
params.params(entry.getKey(), Integer.parseInt(String.valueOf(objectValue)));
} else if (objectValue instanceof Float) {
params.params(entry.getKey(), Float.parseFloat(String.valueOf(objectValue)));
} else if (objectValue instanceof Double) {
params.params(entry.getKey(), Double.parseDouble(String.valueOf(objectValue)));
} else if (objectValue instanceof Long) {
params.params(entry.getKey(), Long.parseLong(String.valueOf(objectValue)));
} else if (objectValue instanceof Character) {
params.params(entry.getKey(), (Character) objectValue);
} else if (objectValue instanceof Boolean) {
params.params(entry.getKey(), Boolean.parseBoolean(String.valueOf(objectValue)));
} else {
LogUtils.Logs_e("body中有不支持的類型");
}
}
}
params.execute(new StringCallback() {
@Override
public void onStart(Request<String, ? extends Request> request) {
super.onStart(request);
if(onRequestExecute != null){
onRequestExecute.onStart();
}
}
@Override
public void onSuccess(Response<String> response) {
LogUtils.Logs_e("接口"+url.substring(url.lastIndexOf("/"))+"請求結果:"+response.body());
if(onRequestExecute != null){
onRequestExecute.onSuccess(response);
}
}
@Override
public void onError(Response<String> response) {
super.onError(response);
LogUtils.Logs_e("地址:"+url+"請求失敗");
if(response != null && response.getException() != null) {
LogUtils.Logs_e("原因:"+response.getException().toString());
}
if(onRequestExecute != null){
onRequestExecute.onError(response);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 下載請求
* @param context 當前對象
* @param downloadDir 下載到的文件夾目錄
* @param saveName 保存成的文件名稱
* @param url 請求地址
* @param bodyValue body鍵值對
* @param onDownloadRequestExecute 請求完成回調
* @author LH
* created at 2019/9/20 10:48
*/
public static void httpDownloadRequest(final Context context, String downloadDir, String saveName, final String url, HashMap<String,Object> bodyValue, final OnDownloadRequestExecute onDownloadRequestExecute){
try {
LogUtils.Logs_e("http請求地址:"+url);
GetRequest<File> params = OkGo.<File>get(url).tag(context.getApplicationContext());
if(bodyValue != null && bodyValue.size() != 0) {
LogUtils.Logs_e("body爲:"+bodyValue.toString());
Set<Map.Entry<String, Object>> bodySet = bodyValue.entrySet();
for (Map.Entry<String, Object> entry : bodySet) {
Object objectValue = entry.getValue();
if (objectValue instanceof String) {
params.params(entry.getKey(), String.valueOf(objectValue));
} else if (objectValue instanceof Integer) {
params.params(entry.getKey(), Integer.parseInt(String.valueOf(objectValue)));
} else if (objectValue instanceof Float) {
params.params(entry.getKey(), Float.parseFloat(String.valueOf(objectValue)));
} else if (objectValue instanceof Double) {
params.params(entry.getKey(), Double.parseDouble(String.valueOf(objectValue)));
} else if (objectValue instanceof Long) {
params.params(entry.getKey(), Long.parseLong(String.valueOf(objectValue)));
} else if (objectValue instanceof Character) {
params.params(entry.getKey(), (Character) objectValue);
} else if (objectValue instanceof Boolean) {
params.params(entry.getKey(), Boolean.parseBoolean(String.valueOf(objectValue)));
} else {
LogUtils.Logs_e("body中有不支持的類型");
}
}
}
File file = new File(downloadDir,saveName);
if (file.exists()) {
file.delete();
}
params.execute(new FileCallback(downloadDir,saveName) {
@Override
public void onStart(Request<File, ? extends Request> request) {
if(onDownloadRequestExecute != null){
onDownloadRequestExecute.onStart();
}
}
@Override
public void onSuccess(Response<File> response) {
LogUtils.Logs_e("接口"+url.substring(url.lastIndexOf("/"))+"請求成功");
if(onDownloadRequestExecute != null){
onDownloadRequestExecute.onSuccess(response);
}
}
@Override
public void onError(Response<File> response) {
LogUtils.Logs_e("地址:"+url+"請求失敗");
if(response != null && response.getException() != null) {
LogUtils.Logs_e("原因:"+response.getException().toString());
}
if(onDownloadRequestExecute != null){
onDownloadRequestExecute.onError(response);
}
}
@Override
public void downloadProgress(Progress progress) {
if(onDownloadRequestExecute != null){
onDownloadRequestExecute.downloadProgress(progress);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 下載請求
* @param context 當前對象
* @param downloadDir 下載到的文件夾目錄
* @param saveName 保存成的文件名稱
* @param url 請求地址
* @param headValue head鍵值對
* @param bodyValue body鍵值對
* @param onDownloadRequestExecute 請求完成回調
* @author LH
* created at 2019/9/20 10:48
*/
public static void httpDownloadRequest(final Context context, String downloadDir, String saveName, final String url, HashMap<String,Object> headValue, HashMap<String,Object> bodyValue, final OnDownloadRequestExecute onDownloadRequestExecute){
try {
LogUtils.Logs_e("http請求地址:"+url);
GetRequest<File> params = OkGo.<File>get(url).tag(context.getApplicationContext());
if(headValue != null && headValue.size() != 0) {
LogUtils.Logs_e("header爲:"+headValue.toString());
HttpHeaders headers = new HttpHeaders();
Set<Map.Entry<String, Object>> headSet = headValue.entrySet();
for (Map.Entry<String, Object> entry : headSet) {
Object objectValue = entry.getValue();
if (objectValue instanceof String) {
headers.put(entry.getKey(), String.valueOf(objectValue));
} else {
LogUtils.Logs_e("header中有不支持的類型");
}
}
params.headers(headers);
}
if(bodyValue != null && bodyValue.size() != 0) {
LogUtils.Logs_e("body爲:"+bodyValue.toString());
Set<Map.Entry<String, Object>> bodySet = bodyValue.entrySet();
for (Map.Entry<String, Object> entry : bodySet) {
Object objectValue = entry.getValue();
if (objectValue instanceof String) {
params.params(entry.getKey(), String.valueOf(objectValue));
} else if (objectValue instanceof Integer) {
params.params(entry.getKey(), Integer.parseInt(String.valueOf(objectValue)));
} else if (objectValue instanceof Float) {
params.params(entry.getKey(), Float.parseFloat(String.valueOf(objectValue)));
} else if (objectValue instanceof Double) {
params.params(entry.getKey(), Double.parseDouble(String.valueOf(objectValue)));
} else if (objectValue instanceof Long) {
params.params(entry.getKey(), Long.parseLong(String.valueOf(objectValue)));
} else if (objectValue instanceof Character) {
params.params(entry.getKey(), (Character) objectValue);
} else if (objectValue instanceof Boolean) {
params.params(entry.getKey(), Boolean.parseBoolean(String.valueOf(objectValue)));
} else {
LogUtils.Logs_e("body中有不支持的類型");
}
}
}
File file = new File(downloadDir,saveName);
if (file.exists()) {
file.delete();
}
params.execute(new FileCallback(downloadDir,saveName) {
@Override
public void onStart(Request<File, ? extends Request> request) {
if(onDownloadRequestExecute != null){
onDownloadRequestExecute.onStart();
}
}
@Override
public void onSuccess(Response<File> response) {
LogUtils.Logs_e("接口"+url.substring(url.lastIndexOf("/"))+"請求成功");
if(onDownloadRequestExecute != null){
onDownloadRequestExecute.onSuccess(response);
}
}
@Override
public void onError(Response<File> response) {
LogUtils.Logs_e("地址:"+url+"請求失敗");
if(response != null && response.getException() != null) {
LogUtils.Logs_e("原因:"+response.getException().toString());
}
if(onDownloadRequestExecute != null){
onDownloadRequestExecute.onError(response);
}
}
@Override
public void downloadProgress(Progress progress) {
if(onDownloadRequestExecute != null){
onDownloadRequestExecute.downloadProgress(progress);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 上傳請求
* @param context 當前對象
* @param url 請求地址
* @param bodyValue body鍵值對
* @param onUploadRequestExecute 請求完成回調
* @author LH
* created at 2019/9/20 10:48
*/
public static void httpUploadRequest(final Context context, final String url, HashMap<String,Object> bodyValue, final OnUploadRequestExecute onUploadRequestExecute){
try {
LogUtils.Logs_e("http請求地址:"+url);
PostRequest<String> params = OkGo.<String>post(url).tag(context.getApplicationContext());
if(bodyValue != null && bodyValue.size() != 0) {
LogUtils.Logs_e("body爲:"+bodyValue.toString());
Set<Map.Entry<String, Object>> bodySet = bodyValue.entrySet();
for (Map.Entry<String, Object> entry : bodySet) {
Object objectValue = entry.getValue();
if (objectValue instanceof String) {
params.params(entry.getKey(), String.valueOf(objectValue));
} else if (objectValue instanceof Integer) {
params.params(entry.getKey(), Integer.parseInt(String.valueOf(objectValue)));
} else if (objectValue instanceof Float) {
params.params(entry.getKey(), Float.parseFloat(String.valueOf(objectValue)));
} else if (objectValue instanceof Double) {
params.params(entry.getKey(), Double.parseDouble(String.valueOf(objectValue)));
} else if (objectValue instanceof Long) {
params.params(entry.getKey(), Long.parseLong(String.valueOf(objectValue)));
} else if (objectValue instanceof Character) {
params.params(entry.getKey(), (Character) objectValue);
} else if (objectValue instanceof Boolean) {
params.params(entry.getKey(), Boolean.parseBoolean(String.valueOf(objectValue)));
}else if (objectValue instanceof File) {
params.params(entry.getKey(), (File)objectValue);
}else {
LogUtils.Logs_e("body中有不支持的類型");
}
}
}
params.execute(new StringCallback() {
@Override
public void onStart(Request<String, ? extends Request> request) {
super.onStart(request);
if(onUploadRequestExecute != null){
onUploadRequestExecute.onStart();
}
}
@Override
public void onSuccess(Response<String> response) {
LogUtils.Logs_e("接口"+url.substring(url.lastIndexOf("/"))+"請求結果:"+response.body());
if(onUploadRequestExecute != null){
onUploadRequestExecute.onSuccess(response);
}
}
@Override
public void onError(Response<String> response) {
super.onError(response);
LogUtils.Logs_e("地址:"+url+"請求失敗");
if(response != null && response.getException() != null) {
LogUtils.Logs_e("原因:"+response.getException().toString());
}
if(onUploadRequestExecute != null){
onUploadRequestExecute.onError(response);
}
}
@Override
public void uploadProgress(Progress progress) {
super.uploadProgress(progress);
if(onUploadRequestExecute != null){
onUploadRequestExecute.uploadProgress(progress);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 上傳請求
* @param context 當前對象
* @param url 請求地址
* @param headValue head鍵值對
* @param bodyValue body鍵值對
* @param onUploadRequestExecute 請求完成回調
* @author LH
* created at 2019/9/20 10:48
*/
public static void httpUploadRequest(final Context context, final String url, HashMap<String,Object> headValue, HashMap<String,Object> bodyValue, final OnUploadRequestExecute onUploadRequestExecute){
try {
LogUtils.Logs_e("http請求地址:"+url);
PostRequest<String> params = OkGo.<String>post(url).tag(context.getApplicationContext());
if(headValue != null && headValue.size() != 0) {
LogUtils.Logs_e("header爲:"+headValue.toString());
HttpHeaders headers = new HttpHeaders();
Set<Map.Entry<String, Object>> headSet = headValue.entrySet();
for (Map.Entry<String, Object> entry : headSet) {
Object objectValue = entry.getValue();
if (objectValue instanceof String) {
headers.put(entry.getKey(), String.valueOf(objectValue));
} else {
LogUtils.Logs_e("header中有不支持的類型");
}
}
params.headers(headers);
}
if(bodyValue != null && bodyValue.size() != 0) {
LogUtils.Logs_e("body爲:"+bodyValue.toString());
Set<Map.Entry<String, Object>> bodySet = bodyValue.entrySet();
for (Map.Entry<String, Object> entry : bodySet) {
Object objectValue = entry.getValue();
if (objectValue instanceof String) {
params.params(entry.getKey(), String.valueOf(objectValue));
} else if (objectValue instanceof Integer) {
params.params(entry.getKey(), Integer.parseInt(String.valueOf(objectValue)));
} else if (objectValue instanceof Float) {
params.params(entry.getKey(), Float.parseFloat(String.valueOf(objectValue)));
} else if (objectValue instanceof Double) {
params.params(entry.getKey(), Double.parseDouble(String.valueOf(objectValue)));
} else if (objectValue instanceof Long) {
params.params(entry.getKey(), Long.parseLong(String.valueOf(objectValue)));
} else if (objectValue instanceof Character) {
params.params(entry.getKey(), (Character) objectValue);
} else if (objectValue instanceof Boolean) {
params.params(entry.getKey(), Boolean.parseBoolean(String.valueOf(objectValue)));
} else if (objectValue instanceof File) {
params.params(entry.getKey(), (File)objectValue);
} else {
LogUtils.Logs_e("body中有不支持的類型");
}
}
}
params.execute(new StringCallback() {
@Override
public void onStart(Request<String, ? extends Request> request) {
super.onStart(request);
if(onUploadRequestExecute != null){
onUploadRequestExecute.onStart();
}
}
@Override
public void onSuccess(Response<String> response) {
LogUtils.Logs_e("接口"+url.substring(url.lastIndexOf("/"))+"請求結果:"+response.body());
if(onUploadRequestExecute != null){
onUploadRequestExecute.onSuccess(response);
}
}
@Override
public void onError(Response<String> response) {
super.onError(response);
LogUtils.Logs_e("地址:"+url+"請求失敗");
if(response != null && response.getException() != null) {
LogUtils.Logs_e("原因:"+response.getException().toString());
}
if(onUploadRequestExecute != null){
onUploadRequestExecute.onError(response);
}
}
@Override
public void uploadProgress(Progress progress) {
super.uploadProgress(progress);
if(onUploadRequestExecute != null){
onUploadRequestExecute.uploadProgress(progress);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 上傳請求
* @param context 當前對象
* @param url 請求地址
* @param bodyValue body鍵值對
* @param filesKey 批量上傳的鍵(對個文件對應同一個key)
* @param uploadFiles 批量上傳的文件數組
* @param onUploadRequestExecute 請求完成回調
* @author LH
* created at 2019/9/20 10:48
*/
public static void httpUploadRequest(final Context context, final String url, HashMap<String,Object> bodyValue, String filesKey, ArrayList<File> uploadFiles, final OnUploadRequestExecute onUploadRequestExecute){
try {
LogUtils.Logs_e("http請求地址:"+url);
PostRequest<String> params = OkGo.<String>post(url).tag(context.getApplicationContext());
if(bodyValue != null && bodyValue.size() != 0) {
LogUtils.Logs_e("body爲:"+bodyValue.toString());
Set<Map.Entry<String, Object>> bodySet = bodyValue.entrySet();
for (Map.Entry<String, Object> entry : bodySet) {
Object objectValue = entry.getValue();
if (objectValue instanceof String) {
params.params(entry.getKey(), String.valueOf(objectValue));
} else if (objectValue instanceof Integer) {
params.params(entry.getKey(), Integer.parseInt(String.valueOf(objectValue)));
} else if (objectValue instanceof Float) {
params.params(entry.getKey(), Float.parseFloat(String.valueOf(objectValue)));
} else if (objectValue instanceof Double) {
params.params(entry.getKey(), Double.parseDouble(String.valueOf(objectValue)));
} else if (objectValue instanceof Long) {
params.params(entry.getKey(), Long.parseLong(String.valueOf(objectValue)));
} else if (objectValue instanceof Character) {
params.params(entry.getKey(), (Character) objectValue);
} else if (objectValue instanceof Boolean) {
params.params(entry.getKey(), Boolean.parseBoolean(String.valueOf(objectValue)));
} else if (objectValue instanceof File) {
params.params(entry.getKey(), (File)objectValue);
} else {
LogUtils.Logs_e("body中有不支持的類型");
}
}
}
params.addFileParams(filesKey,uploadFiles);
params.execute(new StringCallback() {
@Override
public void onStart(Request<String, ? extends Request> request) {
super.onStart(request);
if(onUploadRequestExecute != null){
onUploadRequestExecute.onStart();
}
}
@Override
public void onSuccess(Response<String> response) {
LogUtils.Logs_e("接口"+url.substring(url.lastIndexOf("/"))+"請求結果:"+response.body());
if(onUploadRequestExecute != null){
onUploadRequestExecute.onSuccess(response);
}
}
@Override
public void onError(Response<String> response) {
super.onError(response);
LogUtils.Logs_e("地址:"+url+"請求失敗");
if(response != null && response.getException() != null) {
LogUtils.Logs_e("原因:"+response.getException().toString());
}
if(onUploadRequestExecute != null){
onUploadRequestExecute.onError(response);
}
}
@Override
public void uploadProgress(Progress progress) {
super.uploadProgress(progress);
if(onUploadRequestExecute != null){
onUploadRequestExecute.uploadProgress(progress);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 上傳請求
* @param context 當前對象
* @param url 請求地址
* @param headValue head鍵值對
* @param bodyValue body鍵值對
* @param filesKey 批量上傳的鍵(對個文件對應同一個key)
* @param uploadFiles 批量上傳的文件數組
* @param onUploadRequestExecute 請求完成回調
* @author LH
* created at 2019/9/20 10:48
*/
public static void httpUploadRequest(final Context context, final String url,HashMap<String,Object> headValue, HashMap<String,Object> bodyValue, String filesKey, ArrayList<File> uploadFiles, final OnUploadRequestExecute onUploadRequestExecute){
try {
LogUtils.Logs_e("http請求地址:"+url);
PostRequest<String> params = OkGo.<String>post(url).tag(context.getApplicationContext());
if(headValue != null && headValue.size() != 0) {
LogUtils.Logs_e("header爲:"+headValue.toString());
HttpHeaders headers = new HttpHeaders();
Set<Map.Entry<String, Object>> headSet = headValue.entrySet();
for (Map.Entry<String, Object> entry : headSet) {
Object objectValue = entry.getValue();
if (objectValue instanceof String) {
headers.put(entry.getKey(), String.valueOf(objectValue));
} else {
LogUtils.Logs_e("header中有不支持的類型");
}
}
params.headers(headers);
}
if(bodyValue != null && bodyValue.size() != 0) {
LogUtils.Logs_e("body爲:"+bodyValue.toString());
Set<Map.Entry<String, Object>> bodySet = bodyValue.entrySet();
for (Map.Entry<String, Object> entry : bodySet) {
Object objectValue = entry.getValue();
if (objectValue instanceof String) {
params.params(entry.getKey(), String.valueOf(objectValue));
} else if (objectValue instanceof Integer) {
params.params(entry.getKey(), Integer.parseInt(String.valueOf(objectValue)));
} else if (objectValue instanceof Float) {
params.params(entry.getKey(), Float.parseFloat(String.valueOf(objectValue)));
} else if (objectValue instanceof Double) {
params.params(entry.getKey(), Double.parseDouble(String.valueOf(objectValue)));
} else if (objectValue instanceof Long) {
params.params(entry.getKey(), Long.parseLong(String.valueOf(objectValue)));
} else if (objectValue instanceof Character) {
params.params(entry.getKey(), (Character) objectValue);
} else if (objectValue instanceof Boolean) {
params.params(entry.getKey(), Boolean.parseBoolean(String.valueOf(objectValue)));
} else if (objectValue instanceof File) {
params.params(entry.getKey(), (File)objectValue);
} else {
LogUtils.Logs_e("body中有不支持的類型");
}
}
}
params.addFileParams(filesKey,uploadFiles);
params.execute(new StringCallback() {
@Override
public void onStart(Request<String, ? extends Request> request) {
super.onStart(request);
if(onUploadRequestExecute != null){
onUploadRequestExecute.onStart();
}
}
@Override
public void onSuccess(Response<String> response) {
LogUtils.Logs_e("接口"+url.substring(url.lastIndexOf("/"))+"請求結果:"+response.body());
if(onUploadRequestExecute != null){
onUploadRequestExecute.onSuccess(response);
}
}
@Override
public void onError(Response<String> response) {
super.onError(response);
LogUtils.Logs_e("地址:"+url+"請求失敗");
if(response != null && response.getException() != null) {
LogUtils.Logs_e("原因:"+response.getException().toString());
}
if(onUploadRequestExecute != null){
onUploadRequestExecute.onError(response);
}
}
@Override
public void uploadProgress(Progress progress) {
super.uploadProgress(progress);
if(onUploadRequestExecute != null){
onUploadRequestExecute.uploadProgress(progress);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 請求回調
* @author LH
* created at 2019/9/20 10:43
*/
public interface OnRequestExecute{
void onStart();
void onSuccess(Response<String> response);
void onError(Response<String> response);
}
/**
* 下載請求回調
* @author LH
* created at 2019/9/20 10:43
*/
public interface OnDownloadRequestExecute{
void onStart();
void onSuccess(Response<File> response);
void onError(Response<File> response);
void downloadProgress(Progress progress);
}
/**
* 上傳請求回調
* @author LH
* created at 2019/9/20 10:43
*/
public interface OnUploadRequestExecute{
void onStart();
void onSuccess(Response<String> response);
void onError(Response<String> response);
void uploadProgress(Progress progress);
}
}