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 bodyValue, final OnRequestExecute onRequestExecute){
        try {
            LogUtils.Logs_e("http请求地址:"+url);
            GetRequest params = OkGo.get(url).tag(context.getApplicationContext());
            if(bodyValue != null && bodyValue.size() != 0) {
                LogUtils.Logs_e("body为:"+bodyValue.toString());
                Set> bodySet = bodyValue.entrySet();
                for (Map.Entry 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 request) {
                    super.onStart(request);
                    if(onRequestExecute != null){
                        onRequestExecute.onStart();
                    }
                }

                @Override
                public void onSuccess(Response response) {
                    LogUtils.Logs_e("接口"+url.substring(url.lastIndexOf("/"))+"请求结果:"+response.body());
                    if(onRequestExecute != null){
                        onRequestExecute.onSuccess(response);
                    }
                }

                @Override
                public void onError(Response 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 headValue, HashMap bodyValue, final OnRequestExecute onRequestExecute){
        try {
            LogUtils.Logs_e("http请求地址:"+url);
            GetRequest params = OkGo.get(url).tag(context.getApplicationContext());
            if(headValue != null && headValue.size() != 0) {
                LogUtils.Logs_e("header为:"+headValue.toString());
                Set> headSet = headValue.entrySet();
                HttpHeaders headers = new HttpHeaders();
                for (Map.Entry 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> bodySet = bodyValue.entrySet();
                for (Map.Entry 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 request) {
                    super.onStart(request);
                    if(onRequestExecute != null){
                        onRequestExecute.onStart();
                    }
                }

                @Override
                public void onSuccess(Response response) {
                    LogUtils.Logs_e("接口"+url.substring(url.lastIndexOf("/"))+"请求结果:"+response.body());
                    if(onRequestExecute != null){
                        onRequestExecute.onSuccess(response);
                    }
                }

                @Override
                public void onError(Response 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 bodyValue, final OnRequestExecute onRequestExecute){
        try {
            LogUtils.Logs_e("http请求地址:"+url);
            PostRequest params = OkGo.post(url).tag(context.getApplicationContext());
            if(bodyValue != null && bodyValue.size() != 0) {
                LogUtils.Logs_e("body为:"+bodyValue.toString());
                Set> bodySet = bodyValue.entrySet();
                for (Map.Entry 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 request) {
                    super.onStart(request);
                    if(onRequestExecute != null){
                        onRequestExecute.onStart();
                    }
                }

                @Override
                public void onSuccess(Response response) {
                    LogUtils.Logs_e("接口"+url.substring(url.lastIndexOf("/"))+"请求结果:"+response.body());
                    if(onRequestExecute != null){
                        onRequestExecute.onSuccess(response);
                    }
                }

                @Override
                public void onError(Response 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 headValue, HashMap bodyValue, final OnRequestExecute onRequestExecute){
        try {
            LogUtils.Logs_e("http请求地址:"+url);
            PostRequest params = OkGo.post(url).tag(context.getApplicationContext());
            if(headValue != null && headValue.size() != 0) {
                LogUtils.Logs_e("header为:"+headValue.toString());
                HttpHeaders headers = new HttpHeaders();
                Set> headSet = headValue.entrySet();
                for (Map.Entry 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> bodySet = bodyValue.entrySet();
                for (Map.Entry 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 request) {
                    super.onStart(request);
                    if(onRequestExecute != null){
                        onRequestExecute.onStart();
                    }
                }

                @Override
                public void onSuccess(Response response) {
                    LogUtils.Logs_e("接口"+url.substring(url.lastIndexOf("/"))+"请求结果:"+response.body());
                    if(onRequestExecute != null){
                        onRequestExecute.onSuccess(response);
                    }
                }

                @Override
                public void onError(Response 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 bodyValue, final OnDownloadRequestExecute onDownloadRequestExecute){
        try {
            LogUtils.Logs_e("http请求地址:"+url);
            GetRequest params = OkGo.get(url).tag(context.getApplicationContext());
            if(bodyValue != null && bodyValue.size() != 0) {
                LogUtils.Logs_e("body为:"+bodyValue.toString());
                Set> bodySet = bodyValue.entrySet();
                for (Map.Entry 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 request) {
                    if(onDownloadRequestExecute != null){
                        onDownloadRequestExecute.onStart();
                    }
                }

                @Override
                public void onSuccess(Response response) {
                    LogUtils.Logs_e("接口"+url.substring(url.lastIndexOf("/"))+"请求成功");
                    if(onDownloadRequestExecute != null){
                        onDownloadRequestExecute.onSuccess(response);
                    }
                }

                @Override
                public void onError(Response 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 headValue, HashMap bodyValue, final OnDownloadRequestExecute onDownloadRequestExecute){
        try {
            LogUtils.Logs_e("http请求地址:"+url);
            GetRequest params = OkGo.get(url).tag(context.getApplicationContext());
            if(headValue != null && headValue.size() != 0) {
                LogUtils.Logs_e("header为:"+headValue.toString());
                HttpHeaders headers = new HttpHeaders();
                Set> headSet = headValue.entrySet();
                for (Map.Entry 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> bodySet = bodyValue.entrySet();
                for (Map.Entry 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 request) {
                    if(onDownloadRequestExecute != null){
                        onDownloadRequestExecute.onStart();
                    }
                }

                @Override
                public void onSuccess(Response response) {
                    LogUtils.Logs_e("接口"+url.substring(url.lastIndexOf("/"))+"请求成功");
                    if(onDownloadRequestExecute != null){
                        onDownloadRequestExecute.onSuccess(response);
                    }
                }

                @Override
                public void onError(Response 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 bodyValue, final OnUploadRequestExecute onUploadRequestExecute){
        try {
            LogUtils.Logs_e("http请求地址:"+url);
            PostRequest params = OkGo.post(url).tag(context.getApplicationContext());
            if(bodyValue != null && bodyValue.size() != 0) {
                LogUtils.Logs_e("body为:"+bodyValue.toString());
                Set> bodySet = bodyValue.entrySet();
                for (Map.Entry 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 request) {
                    super.onStart(request);
                    if(onUploadRequestExecute != null){
                        onUploadRequestExecute.onStart();
                    }
                }

                @Override
                public void onSuccess(Response response) {
                    LogUtils.Logs_e("接口"+url.substring(url.lastIndexOf("/"))+"请求结果:"+response.body());
                    if(onUploadRequestExecute != null){
                        onUploadRequestExecute.onSuccess(response);
                    }
                }

                @Override
                public void onError(Response 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 headValue, HashMap bodyValue, final OnUploadRequestExecute onUploadRequestExecute){
        try {
            LogUtils.Logs_e("http请求地址:"+url);
            PostRequest params = OkGo.post(url).tag(context.getApplicationContext());
            if(headValue != null && headValue.size() != 0) {
                LogUtils.Logs_e("header为:"+headValue.toString());
                HttpHeaders headers = new HttpHeaders();
                Set> headSet = headValue.entrySet();
                for (Map.Entry 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> bodySet = bodyValue.entrySet();
                for (Map.Entry 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 request) {
                    super.onStart(request);
                    if(onUploadRequestExecute != null){
                        onUploadRequestExecute.onStart();
                    }
                }

                @Override
                public void onSuccess(Response response) {
                    LogUtils.Logs_e("接口"+url.substring(url.lastIndexOf("/"))+"请求结果:"+response.body());
                    if(onUploadRequestExecute != null){
                        onUploadRequestExecute.onSuccess(response);
                    }
                }

                @Override
                public void onError(Response 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 bodyValue, String filesKey, ArrayList uploadFiles, final OnUploadRequestExecute onUploadRequestExecute){
        try {
            LogUtils.Logs_e("http请求地址:"+url);
            PostRequest params = OkGo.post(url).tag(context.getApplicationContext());
            if(bodyValue != null && bodyValue.size() != 0) {
                LogUtils.Logs_e("body为:"+bodyValue.toString());
                Set> bodySet = bodyValue.entrySet();
                for (Map.Entry 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 request) {
                    super.onStart(request);
                    if(onUploadRequestExecute != null){
                        onUploadRequestExecute.onStart();
                    }
                }

                @Override
                public void onSuccess(Response response) {
                    LogUtils.Logs_e("接口"+url.substring(url.lastIndexOf("/"))+"请求结果:"+response.body());
                    if(onUploadRequestExecute != null){
                        onUploadRequestExecute.onSuccess(response);
                    }
                }

                @Override
                public void onError(Response 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 headValue,  HashMap bodyValue, String filesKey, ArrayList uploadFiles, final OnUploadRequestExecute onUploadRequestExecute){
        try {
            LogUtils.Logs_e("http请求地址:"+url);
            PostRequest params = OkGo.post(url).tag(context.getApplicationContext());
            if(headValue != null && headValue.size() != 0) {
                LogUtils.Logs_e("header为:"+headValue.toString());
                HttpHeaders headers = new HttpHeaders();
                Set> headSet = headValue.entrySet();
                for (Map.Entry 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> bodySet = bodyValue.entrySet();
                for (Map.Entry 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 request) {
                    super.onStart(request);
                    if(onUploadRequestExecute != null){
                        onUploadRequestExecute.onStart();
                    }
                }

                @Override
                public void onSuccess(Response response) {
                    LogUtils.Logs_e("接口"+url.substring(url.lastIndexOf("/"))+"请求结果:"+response.body());
                    if(onUploadRequestExecute != null){
                        onUploadRequestExecute.onSuccess(response);
                    }
                }

                @Override
                public void onError(Response 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 response);
        void onError(Response response);
    }

    /**
     * 下载请求回调
     * @author LH
     * created at 2019/9/20 10:43
     */
    public interface OnDownloadRequestExecute{
        void onStart();
        void onSuccess(Response response);
        void onError(Response response);
        void downloadProgress(Progress progress);
    }

    /**
     * 上传请求回调
     * @author LH
     * created at 2019/9/20 10:43
     */
    public interface OnUploadRequestExecute{
        void onStart();
        void onSuccess(Response response);
        void onError(Response response);
        void uploadProgress(Progress progress);
    }
}

 

你可能感兴趣的:(android工具类)