Android okgoUtils

package cn.qbang.rpc.net;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.qbang.rpc.Constant.Constant;
import cn.qbang.rpc.utils.LogUtil;
import cn.qbang.rpc.utils.ShareUtils;

/**
 * 功能:网络请求框架的封装
 * Yunhao Zhang on 20190326
 */

public class OkGoUtils {
    /**
     * 对okGo的post请求进行封装
     *
     * @param activity       上下文
     * @param urL            接口地址
     * @param httpParams     参数的json数据
     * @param responseResult 请求结果的接口
     */


    public static void OkGoPost(final Activity activity
            , final String urL
            , HttpParams httpParams
            , final ResponseResult responseResult) {
        Map map = new HashMap();
        LinkedHashMap> paramsMap = httpParams.urlParamsMap;
        if (paramsMap.size() > 0) {
            for (String key : paramsMap.keySet()) {
                if (paramsMap.get(key).get(0) != null) {
                    map.put(key, paramsMap.get(key).get(0));
                }
            }
        }
        String json = new Gson().toJson(map);
        String token = ShareUtils.getString(activity, "token", "");
        LogUtil.e(token);
        LogUtil.e(json);
        if (null != activity) {
            final DialogLoading.Builder builder = new DialogLoading.Builder(activity);
            builder.create();
            builder.show();
            OkGo.post(Constant.BASE_URL + urL)
                    .tag(activity)
                    .headers("Authorization", "Bearer " + token)
                    .headers("language", "zh-CN")
                    .headers("platform", "Android")
                    .headers("Accept", "application/vnd.rpc.v2+json")
                    .upJson(json)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response response) {
                            Log.e("使用post请求网络返回成功的结果:", "onSuccess: " + response.body() + "请求网络地址:" + urL);
                            NormalNoListBean normalListBean = new Gson().fromJson(response.body(), NormalNoListBean.class);
                            switch (normalListBean.getCode()) {
                                case 2000://请求操作成功
                                    responseResult.Success(response);
                                    builder.hint();
                                    if (response.body().contains("wait")) {

                                    }
                                    break;
                                case 3100://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "帐号或密码无效", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3009://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "密码错误或无效", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3101://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "帐号无效或不符合要求", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3102://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "帐号已被其他用户使用", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3103://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "帐号不存在", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3104://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "帐号已被禁用", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3105://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "验证无效或已过期", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3107://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "验证发送已超出限制,请1小时后再试", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3108://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "验证码发送操作过于频繁,请等待", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3109://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "状态非法", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3110://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "需要关注公众号", Toast.LENGTH_SHORT).show();
                                    break;
                                case 5101://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "无效或非法设备", Toast.LENGTH_SHORT).show();
                                    break;
                                case 5102:
                                    builder.hint();
                                    Toast.makeText(activity, "设备已被其他用户使用", Toast.LENGTH_SHORT).show();
                                    break;
                                case 2001:
                                    builder.hint();
                                    Toast.makeText(activity, "操作失败", Toast.LENGTH_SHORT).show();
                                    break;
                                case 2002:
                                    builder.hint();
                                    Toast.makeText(activity, "参数错误", Toast.LENGTH_SHORT).show();
                                    break;
                                case 2003:
                                    builder.hint();
                                    Toast.makeText(activity, "接口不存在", Toast.LENGTH_SHORT).show();
                                    break;
                                case 2004:
                                    builder.hint();
                                    Toast.makeText(activity, "请求方式不支持", Toast.LENGTH_SHORT).show();
                                    break;
                                case 2005://登录的token失效
                                    builder.hint();
                                    //  ToastUtil.showLongTextToast(activity.getResources().getString(R.string.tokenshixiao));
                                    //  new TokenOutTimeUtlis(activity).intentToLogin();
                                    break;

                                case 1000:
                                    builder.hint();
                                    Toast.makeText(activity, "未知错误", Toast.LENGTH_SHORT).show();
                                    break;
                                case 1001:
                                    builder.hint();
                                    Toast.makeText(activity, "系统错误", Toast.LENGTH_SHORT).show();
                                    break;
                                case 1002:
                                    builder.hint();
                                    Toast.makeText(activity, "服务不可用", Toast.LENGTH_SHORT).show();
                                    break;
                                case 1003:
                                    builder.hint();
                                    Toast.makeText(activity, "远程服务错误", Toast.LENGTH_SHORT).show();
                                    break;
                                default:
                                    builder.hint();
                                    responseResult.SuccessZero(response);
                                    break;

                            }
                        }

                        @Override
                        public void onError(Response response) {
                            super.onError(response);
                            builder.hint();
                            Log.e("使用post网络请求失败的原因:", "onError: " + response.message() + response.body() + response.code() + "请求失败的地址是:" + urL);
                            responseResult.Error(response);

                        }
                    });
        }
    }

    /**
     * 对okGo的post请求进行封装
     *
     * @param activity       上下文
     * @param urL            接口地址
     * @param httpParams     参数的json数据
     * @param responseResult 请求结果的接口
     */


    public static void OkGoNoPost(final Activity activity
            , final String urL
            , HttpParams httpParams
            , final ResponseResult responseResult) {
        String token = ShareUtils.getString(activity, "token", "");
        if (null != activity) {
            final DialogLoading.Builder builder = new DialogLoading.Builder(activity);
            builder.create();
            builder.show();
            OkGo.post(Constant.BASE_URL + urL)
                    .tag(activity)
                    .headers("Authorization", "Bearer " + token)
                    .headers("platform", "Android")
                    .headers("Accept", "application/vnd.rpc.v2+json")
                    .headers("language", "zh-CN")
                    .params(httpParams)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response response) {
                            Log.e("使用post请求网络返回成功的结果:", "onSuccess: " + response.body() + "请求网络地址:" + urL);
                            NormalNoListBean normalListBean = new Gson().fromJson(response.body(), NormalNoListBean.class);
                            switch (normalListBean.getCode()) {
                                case 2000://请求操作成功
                                    responseResult.Success(response);
                                    builder.hint();
                                    if (response.body().contains("wait")) {

                                    }

                                    break;
                                case 3100://请求操作成功
                                    builder.hint();
                                    //  ToastUtil.showLongTextToast(activity.getResources().getString(R.string.zhanghuhuomimawuxiao));
                                    break;
                                case 3101://请求操作成功
                                    builder.hint();
                                    // ToastUtil.showLongTextToast(activity.getResources().getString(R.string.zhanghuhuomimawuxiao));
                                    break;
                                case 3102://请求操作成功
                                    builder.hint();
                                    //  ToastUtil.showLongTextToast(activity.getResources().getString(R.string.zhanghuyibeiqitayonghushiyong));
                                    break;
                                case 3103://请求操作成功
                                    builder.hint();
                                    responseResult.SuccessZero(response);
                                    // ToastUtil.showLongTextToast(activity.getResources().getString(R.string.zhanghubucunzai));
                                    break;
                                case 3104://请求操作成功
                                    builder.hint();
                                    //ToastUtil.showLongTextToast(activity.getResources().getString(R.string.zhanghaoyibeijinyong));
                                    break;
                                case 3105://请求操作成功
                                    builder.hint();
                                    //  ToastUtil.showLongTextToast(activity.getResources().getString(R.string.yanzhengwuxiaohuoyiguoqi));
                                    break;
                                case 3107://请求操作成功
                                    builder.hint();
                                    // ToastUtil.showLongTextToast(activity.getResources().getString(R.string.yanzhengfasongyicaoguo));
                                    break;
                                case 3108://请求操作成功
                                    builder.hint();
                                    //  ToastUtil.showLongTextToast(activity.getResources().getString(R.string.yanzhengmafasongguoyupinfan));
                                    break;
                                case 3109://请求操作成功
                                    builder.hint();
                                    //   ToastUtil.showLongTextToast(activity.getResources().getString(R.string.zhuangtaifeifa));
                                    break;
                                case 3110://请求操作成功
                                    builder.hint();
                                    //  ToastUtil.showLongTextToast(activity.getResources().getString(R.string.xuyaoguanzhugongzhonghao));
                                    break;
                                case 5101://请求操作成功
                                    builder.hint();
                                    //   ToastUtil.showLongTextToast(activity.getResources().getString(R.string.wuxiaohuofeifashebei));
                                    break;
                                case 5102:
                                    builder.hint();
                                    //  ToastUtil.showLongTextToast(activity.getResources().getString(R.string.shebeiyibeiqitayonghushiyong));
                                    break;
                                case 2001:
                                    builder.hint();
                                    //   ToastUtil.showLongTextToast(activity.getResources().getString(R.string.caozuoshibai));
                                    break;

                                case 2005://登录的token失效
                                    builder.hint();
                                    //  ToastUtil.showLongTextToast(activity.getResources().getString(R.string.tokenshixiao));
                                    //  new TokenOutTimeUtlis(activity).intentToLogin();
//
                                    break;

                                case 1000:
                                    builder.hint();
                                    //   ToastUtil.showLongTextToast(activity.getResources().getString(R.string.weizhicuowu));
                                    break;
                                case 1001:

                                    //  ToastUtil.showLongTextToast(activity.getResources().getString(R.string.xitongcuowu));
                                    break;
                                case 1002:
                                    builder.hint();
                                    //  ToastUtil.showLongTextToast(activity.getResources().getString(R.string.fuwubukeyong));
                                    break;
                                case 1003:
                                    builder.hint();
                                    //  ToastUtil.showLongTextToast(activity.getResources().getString(R.string.yuanchengfuwucuowu));
                                    break;
                                default:
                                    builder.hint();
                                    responseResult.SuccessZero(response);
                                    break;

                            }
                        }

                        @Override
                        public void onError(Response response) {
                            super.onError(response);

                            Log.e("使用post网络请求失败的原因:", "onError: " + response.message() + response.body() + response.code() + "请求失败的地址是:" + urL);
                            responseResult.Error(response);

                        }
                    });
        }
    }
 /*   public static void OkGoUpdateIcon(
            final Activity activity
            , final String urL
            , HttpParams httpParams
            , final ResponseResult responseResult){
        String token = ShareUtils.getString(activity, "token", "");
        MyLoadDialog.show(activity);
        OkGo.post(Api.BASE_URL + "profile/avatar")
                .tag(activity)
                .headers("language", "zh-CN")
                .headers("platform", "Android")
                .headers("Authorization", "Bearer " + token)
                .headers("Accept", "application/vnd.rpc.v2+json")
                .params(httpParams)
                .isMultipart(true)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response response) {
                        LogUtil.e("上传成功" + response.body());
                    }

                    @Override
                    public void onError(Response response) {
                        LogUtil.e("上传失败" + response.body());
                    }
                });
    }*/


    /**
     * 对okGo的get请求进行封装
     *
     * @param activity       上下文
     * @param urL            接口地址
     * @param httpParams     参数的json数据
     * @param responseResult 请求结果的接口
     */


    public static void OkGoGet(final Activity activity
            , final String urL
            , HttpParams httpParams
            , final ResponseResult responseResult) {
        String json = new Gson().toJson(httpParams);
        LogUtil.e(json);
        String token = ShareUtils.getString(activity, "token", "");
        if (null != activity) {
            final DialogLoading.Builder builder = new DialogLoading.Builder(activity);
            builder.create();
            builder.show();
            OkGo.get(Constant.BASE_URL + urL)
                    .tag(activity)
                    .headers("Authorization", "Bearer " + token)
                    .headers("platform", "Android")
                    .headers("language", "zh-CN")
                    .headers("Accept", "application/vnd.rpc.v2+json")
                    .params(httpParams)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response response) {
                            String body = response.body();
                            LogUtil.e(body);
                            Log.e("使用get请求网络返回成功的结果:", "onSuccess: " + "请求网络地址:" + Constant.BASE_URL + urL + response.body());
                            NormalNoListBean normalListBean = new Gson().fromJson(response.body(), NormalNoListBean.class);
                            switch (normalListBean.getCode()) {
                                case 2000://请求操作成功
                                    responseResult.Success(response);
                                    builder.hint();
                                    /* //测试阶段使用  todo  发布的时候不能用
                                    if (response.body().contains("wait")) {
                                        try {//todo 测试阶段使用
//                                            YZMBean yzmBean = new Gson().fromJson(response.body(), YZMBean.class);
//                                            ToastUtil.showLongTextToast(yzmBean.getData().getCode());
                                        } catch (JsonSyntaxException e) {
                                            e.printStackTrace();

                                        }
                                    }
*/
                                    break;
                                case 3100://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "帐号或密码无效", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3009://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "密码错误或无效", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3101://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "帐号无效或不符合要求", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3102://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "帐号已被其他用户使用", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3103://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "帐号不存在", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3104://请求操作成功
                                    Toast.makeText(activity, "帐号已被禁用", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3105://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "验证无效或已过期", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3107://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "验证发送已超出限制,请1小时后再试", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3108://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "验证码发送操作过于频繁,请等待", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3109://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "状态非法", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3110://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "需要关注公众号", Toast.LENGTH_SHORT).show();
                                    break;
                                case 5101://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "无效或非法设备", Toast.LENGTH_SHORT).show();
                                    break;
                                case 5102:
                                    builder.hint();
                                    Toast.makeText(activity, "设备已被其他用户使用", Toast.LENGTH_SHORT).show();
                                    break;
                                case 2001:
                                    builder.hint();
                                    Toast.makeText(activity, "操作失败", Toast.LENGTH_SHORT).show();
                                    break;
                                case 2002:
                                    builder.hint();
                                    Toast.makeText(activity, "参数错误", Toast.LENGTH_SHORT).show();
                                    break;
                                case 2003:
                                    builder.hint();
                                    Toast.makeText(activity, "接口不存在", Toast.LENGTH_SHORT).show();
                                    break;
                                case 2004:
                                    builder.hint();
                                    Toast.makeText(activity, "请求方式不支持", Toast.LENGTH_SHORT).show();
                                    break;
                                case 2005://登录的token失效
                                    builder.hint();
                                    //  ToastUtil.showLongTextToast(activity.getResources().getString(R.string.tokenshixiao));
                                    //  new TokenOutTimeUtlis(activity).intentToLogin();
                                    break;

                                case 1000:
                                    builder.hint();
                                    Toast.makeText(activity, "未知错误", Toast.LENGTH_SHORT).show();
                                    break;
                                case 1001:
                                    builder.hint();
                                    Toast.makeText(activity, "系统错误", Toast.LENGTH_SHORT).show();
                                    break;
                                case 1002:
                                    builder.hint();
                                    Toast.makeText(activity, "服务不可用", Toast.LENGTH_SHORT).show();
                                    break;
                                case 1003:
                                    builder.hint();
                                    Toast.makeText(activity, "远程服务错误", Toast.LENGTH_SHORT).show();
                                    break;
                                default:
                                    builder.hint();
                                    responseResult.SuccessZero(response);
                                    break;

                            }
                        }

                        @Override
                        public void onError(Response response) {
                            super.onError(response);
                            Log.e("使用post网络请求失败的原因:", "onError: " + response.message() + response.body() + response.code() + "请求失败的地址是:" + urL);
                            responseResult.Error(response);

                        }
                    });
        }
    }

    /**
     * 对okGo的post请求进行封装
     *
     * @param activity       上下文
     * @param urL            接口地址
     * @param httpParams     参数的json数据
     * @param responseResult 请求结果的接口
     */


    public static void OkGoPostArray(final Activity activity
            , final String urL
            , Object httpParams
            , final ResponseResult responseResult) {
        String json = "";
        Map map = new HashMap();
        if (httpParams instanceof HttpParams) {
            LinkedHashMap> paramsMap = ((HttpParams) httpParams).urlParamsMap;
            if (paramsMap.size() > 0) {
                for (String key : paramsMap.keySet()) {
                    if (paramsMap.get(key).get(0) != null) {
                        map.put(key, paramsMap.get(key).get(0));
                    }
                }
            }
            json = new Gson().toJson(map);
        } else if (httpParams instanceof String) {
            json = (String) httpParams;
        } else if (httpParams instanceof Map) {
            json = new Gson().toJson(httpParams);
        }
        //json = new Gson().toJson(map);
        LogUtil.e(json);
        String token = ShareUtils.getString(activity, "token", "");
        if (null != activity) {
            final DialogLoading.Builder builder = new DialogLoading.Builder(activity);
            builder.create();
            builder.show();
            OkGo.post(Constant.BASE_URL + urL)
                    .tag(activity)
                    .headers("Authorization", "Bearer " + token)
                    .headers("platform", "Android")
                    .headers("language", "zh-CN")
                    .headers("Accept", "application/vnd.rpc.v2+json")
                    .upJson(json)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response response) {
                            Log.e("使用post请求网络返回成功的结果:", "onSuccess: " + response.body() + "请求网络地址:" + Constant.BASE_URL + urL);
                            NormalArrayBean normalListBean = new Gson().fromJson(response.body(), NormalArrayBean.class);
                            switch (normalListBean.getCode()) {
                                case 2000://请求操作成功
                                    responseResult.Success(response);
                                    builder.hint();
                                    break;
                                case 3100://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "帐号或密码无效", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3009://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "密码错误或无效", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3101://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "帐号无效或不符合要求", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3102://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "帐号已被其他用户使用", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3103://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "帐号不存在", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3104://请求操作成功
                                    Toast.makeText(activity, "帐号已被禁用", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3105://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "验证无效或已过期", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3107://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "验证发送已超出限制,请1小时后再试", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3108://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "验证码发送操作过于频繁,请等待", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3109://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "状态非法", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3110://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "需要关注公众号", Toast.LENGTH_SHORT).show();
                                    break;
                                case 5101://请求操作成功
                                    builder.hint();
                                    Toast.makeText(activity, "无效或非法设备", Toast.LENGTH_SHORT).show();
                                    break;
                                case 5102:
                                    builder.hint();
                                    Toast.makeText(activity, "设备已被其他用户使用", Toast.LENGTH_SHORT).show();
                                    break;
                                case 2001:
                                    builder.hint();
                                    Toast.makeText(activity, normalListBean.getMsg(), Toast.LENGTH_SHORT).show();
                                    break;
                                case 2002:
                                    builder.hint();
                                    Toast.makeText(activity, "参数错误", Toast.LENGTH_SHORT).show();
                                    break;
                                case 2003:
                                    builder.hint();
                                    Toast.makeText(activity, "接口不存在", Toast.LENGTH_SHORT).show();
                                    break;
                                case 2004:
                                    builder.hint();
                                    Toast.makeText(activity, "请求方式不支持", Toast.LENGTH_SHORT).show();
                                    break;
                                case 2005://登录的token失效
                                    builder.hint();
                                    //  ToastUtil.showLongTextToast(activity.getResources().getString(R.string.tokenshixiao));
                                    //  new TokenOutTimeUtlis(activity).intentToLogin();
                                    break;

                                case 1000:
                                    builder.hint();
                                    Toast.makeText(activity, "未知错误", Toast.LENGTH_SHORT).show();
                                    break;
                                case 1001:
                                    builder.hint();
                                    Toast.makeText(activity, "系统错误", Toast.LENGTH_SHORT).show();
                                    break;
                                case 1002:
                                    builder.hint();
                                    Toast.makeText(activity, "服务不可用", Toast.LENGTH_SHORT).show();
                                    break;
                                case 1003:
                                    builder.hint();
                                    Toast.makeText(activity, "远程服务错误", Toast.LENGTH_SHORT).show();
                                    break;
                            }
                        }

                        @Override
                        public void onError(Response response) {
                            super.onError(response);
                            builder.hint();
                            Log.e("使用post网络请求失败的原因:", "onError: " + response.message() + response.body() + response.code() + "请求失败的地址是:" + urL);
                            responseResult.Error(response);

                        }
                    });
        }
    }


    /**
     * 对okGo的post请求进行封装
     *
     * @param activity       上下文
     * @param urL            接口地址
     * @param httpParams     参数的json数据
     * @param responseResult 请求结果的接口
     */


    public static void OkGoGetArray(final Activity activity
            , final String urL
            , HttpParams httpParams
            , final ResponseResult responseResult) {
        String token = ShareUtils.getString(activity, "token", "");
        Log.e("get获取相关的设备", "OkGoGetArray: " + token);
        if (null != activity) {
            final DialogLoading.Builder builder = new DialogLoading.Builder(activity);
            builder.create();
            builder.show();
            OkGo.get(Constant.BASE_URL + urL)
                    .tag(activity)
                    .headers("Authorization", "Bearer " + token)
                    .headers("platform", "Android")
                    .headers("Accept", "application/vnd.rpc.v2+json")
                    .headers("language", "zh-CN")
                    .params(httpParams)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response response) {
                            Log.e("使用post请求网络返回成功的结果:", "onSuccess: " + response.body() + "请求网络地址:" + urL);
                            NormalArrayBean normalListBean = new Gson().fromJson(response.body(), NormalArrayBean.class);
                            switch (normalListBean.getCode()) {
                                case 2000://请求操作成功
                                    builder.hint();
                                    responseResult.Success(response);
                                    break;
                                case 3100://请求操作成功
                                    builder.hint();
                                    break;
                                case 3101://请求操作成功
                                    builder.hint();
                                    break;
                                case 3102://请求操作成功
                                    builder.hint();
                                    break;
                                case 3103://请求操作成功
                                    builder.hint();
                                    break;
                                case 3104://请求操作成功
                                    break;
                                case 3105://请求操作成功
                                    builder.hint();
                                    break;
                                case 3107://请求操作成功
                                    builder.hint();
                                    break;
                                case 3108://请求操作成功
                                    builder.hint();
                                    break;
                                case 3109://请求操作成功
                                    builder.hint();
                                    break;
                                case 3110://请求操作成功
                                    builder.hint();
                                    break;
                                case 5101://请求操作成功
                                    builder.hint();
                                    break;
                                case 5102:
                                    builder.hint();
                                    break;
                                case 2001:
                                    builder.hint();
                                    break;

                                case 2005://登录的token失效  MyLoadDialog.dismiss(activity);
                                  /*  ToastUtil.showLongTextToast(activity.getResources().getString(R.string.tokenshixiao));
                                    new TokenOutTimeUtlis(activity).intentToLogin();*/
                                    builder.hint();
                                    break;

                                case 1000:
                                    builder.hint();
                                    break;
                                case 1001:
                                    builder.hint();
                                    break;
                                case 1002:
                                    builder.hint();
                                    break;
                                case 1003:
                                    builder.hint();
                                    break;
                            }
                        }

                        @Override
                        public void onError(Response response) {
                            super.onError(response);
                            Log.e("使用post网络请求失败的原因:", "onError: " + response.message() + response.body() + response.code() + "请求失败的地址是:" + urL);
                            responseResult.Error(response);

                        }
                    });
        }
    }

    /**
     * 对okGo的post请求进行封装
     *
     * @param activity       上下文
     * @param urL            接口地址
     * @param httpParams     参数的json数据
     * @param responseResult 请求结果的接口
     */


    public static void OkGoNoTokenPost(final Activity activity
            , final String urL
            , HttpParams httpParams
            , final ResponseResult responseResult) {
        Map map = new HashMap();
        LinkedHashMap> paramsMap = httpParams.urlParamsMap;
        if (paramsMap.size() > 0) {
            for (String key : paramsMap.keySet()) {
                if (paramsMap.get(key).get(0) != null) {
                    map.put(key, paramsMap.get(key).get(0));
                }
            }
        }
        String json = new Gson().toJson(map);
        LogUtil.e(json);
        if (null != activity) {
            final DialogLoading.Builder builder = new DialogLoading.Builder(activity);
            builder.create();
            builder.show();
            OkGo.post(Constant.BASE_URL + urL)
                    .tag(activity)
                    .headers("language", "zh-CN")
                    .headers("platform", "Android")
                    .headers("Accept", "application/vnd.rpc.v2+json")
                    .upJson(json)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response response) {
                            Log.e("使用post请求网络返回成功的结果:", "onSuccess: " + response.body() + "请求成功的地址是:" + Constant.BASE_URL + urL);
                            NormalNoListBean normalListBean = new Gson().fromJson(response.body(), NormalNoListBean.class);
                            switch (normalListBean.getCode()) {
                                case 2000:
                                    responseResult.Success(response);
                                    builder.hint();
                                    break;
                                case 3100://请求操作成功
                                    Toast.makeText(activity, "帐号或密码无效", Toast.LENGTH_SHORT).show();
                                    builder.hint();
                                    break;
                                case 3009://请求操作成功
                                    Toast.makeText(activity, "密码错误或无效", Toast.LENGTH_SHORT).show();
                                    builder.hint();
                                    break;
                                case 3101://请求操作成功
                                    Toast.makeText(activity, "帐号无效或不符合要求", Toast.LENGTH_SHORT).show();
                                    builder.hint();
                                    break;
                                case 3102://请求操作成功
                                    Toast.makeText(activity, "帐号已被其他用户使用", Toast.LENGTH_SHORT).show();
                                    builder.hint();
                                    break;
                                case 3103://请求操作成功
                                    Toast.makeText(activity, "帐号不存在", Toast.LENGTH_SHORT).show();
                                    builder.hint();
                                    break;
                                case 3104://请求操作成功
                                    Toast.makeText(activity, "帐号已被禁用", Toast.LENGTH_SHORT).show();
                                    builder.hint();
                                    break;
                                case 3105://请求操作成功
                                    Toast.makeText(activity, "验证无效或已过期", Toast.LENGTH_SHORT).show();
                                    builder.hint();
                                    break;
                                case 3107://请求操作成功
                                    Toast.makeText(activity, "验证发送已超出限制,请1小时后再试", Toast.LENGTH_SHORT).show();
                                    builder.hint();
                                    break;
                                case 3108://请求操作成功
                                    Toast.makeText(activity, "验证码发送操作过于频繁,请等待", Toast.LENGTH_SHORT).show();
                                    builder.hint();
                                    break;
                                case 3109://请求操作成功
                                    Toast.makeText(activity, "状态非法", Toast.LENGTH_SHORT).show();
                                    builder.hint();
                                    break;
                                case 3110://请求操作成功
                                    Toast.makeText(activity, "需要关注公众号", Toast.LENGTH_SHORT).show();
                                    builder.hint();
                                    break;
                                case 5101://请求操作成功
                                    Toast.makeText(activity, "无效或非法设备", Toast.LENGTH_SHORT).show();
                                    builder.hint();
                                    break;
                                case 5102:
                                    Toast.makeText(activity, "设备已被其他用户使用", Toast.LENGTH_SHORT).show();
                                    builder.hint();
                                    break;
                                case 2001:
                                    Toast.makeText(activity, "操作失败" + normalListBean.getMsg(), Toast.LENGTH_SHORT).show();
                                    builder.hint();
                                    break;
                                case 2002:
                                    Toast.makeText(activity, "参数错误", Toast.LENGTH_SHORT).show();
                                    builder.hint();
                                    break;
                                case 2003:
                                    Toast.makeText(activity, "接口不存在", Toast.LENGTH_SHORT).show();
                                    builder.hint();
                                    break;
                                case 2004:
                                    Toast.makeText(activity, "请求方式不支持", Toast.LENGTH_SHORT).show();
                                    builder.hint();
                                    break;
                                case 2005://登录的token失效
                                    //  ToastUtil.showLongTextToast(activity.getResources().getString(R.string.tokenshixiao));
                                    //  new TokenOutTimeUtlis(activity).intentToLogin();
                                    builder.hint();
                                    break;

                                case 1000:
                                    Toast.makeText(activity, "未知错误", Toast.LENGTH_SHORT).show();
                                    builder.hint();
                                    break;
                                case 1001:
                                    Toast.makeText(activity, "系统错误", Toast.LENGTH_SHORT).show();
                                    builder.hint();
                                    break;
                                case 1002:
                                    Toast.makeText(activity, "服务不可用", Toast.LENGTH_SHORT).show();
                                    builder.hint();
                                    break;
                                case 1003:
                                    Toast.makeText(activity, "远程服务错误", Toast.LENGTH_SHORT).show();
                                    builder.hint();
                                    break;
                                case 2007:
                                    responseResult.Success(response);
                                    builder.hint();
                                    break;
                            }
                        }

                        @Override
                        public void onError(Response response) {
                            super.onError(response);
                            Log.e("使用post网络请求失败的原因:", "onError: " + response.message() + response.body() + response.code() + "请求失败的地址是:" + urL);
                            responseResult.Error(response);

                        }
                    });
        }
    }

    /**
     * 处理请求成功和失败的接口
     */

    public interface ResponseResult {
        void Success(Response response);//网络等于2000的时候的返回

        void SuccessZero(Response response);

        void Error(Response response);//访问出错误的返回

    }
}

你可能感兴趣的:(Android okgoUtils)