OkGo二次封装工具类

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);
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值