OkGo基本使用记录

首先在项目中添加依赖
compile 'com.lzy.net:okgo:3.0.4'

创建加载Dialog

/**
 * 自定义的加载进度对话框
 */
public class CustomProgressDialog extends Dialog {
    private static CustomProgressDialog customProgressDialog = null;

    public CustomProgressDialog(Context context, int theme) {
        super(context, theme);
    }

    public static CustomProgressDialog createDialog(Context context) {
        customProgressDialog = new CustomProgressDialog(context, R.style.CustomProgressDialog);
        customProgressDialog.setContentView(R.layout.dialog_loading_view);
        customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        customProgressDialog.setMessage("加载中,请稍后...");// 默认提示语
        customProgressDialog.setCanceledOnTouchOutside(false);//是否允许点击弹窗外围取消弹窗
        return customProgressDialog;
    }

    public void onWindowFocusChanged(boolean hasFocus) {
        if (customProgressDialog == null) {
            return;
        }
        ProgressBar progressBar = (ProgressBar) customProgressDialog.findViewById(R.id.progressBar);
        progressBar.setVisibility(View.VISIBLE);
    }

    /**
     * setMessage 提示内容
     */
    public CustomProgressDialog setMessage(String strMessage) {
        TextView tvMsg = (TextView) customProgressDialog.findViewById(R.id.tv_loadingmsg);
        if (tvMsg != null) {
            tvMsg.setText(strMessage);
        }
        return customProgressDialog;
    }
}

/**
 * 加载对话框工具类
 */
public class LoadingUtil {
    public CustomProgressDialog progressDialog;

    /**
     * 方法说明 对话框显示
     */
    public void showProgressDialog(Context context) {
        try {
            if (null == progressDialog) {
                progressDialog = CustomProgressDialog.createDialog(context);
            }
            if (!progressDialog.isShowing()) {
                progressDialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showProgressDialog(Context context, String messager) {
        try {
            if (null == progressDialog) {
                progressDialog = CustomProgressDialog.createDialog(context);
            }
            if (!progressDialog.isShowing()) {
                progressDialog.setMessage(messager);
                progressDialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 方法说明 对话框关闭
     */
    public void dismissProgressDialog() {
        try {
            if (null != progressDialog && progressDialog.isShowing()) {
                progressDialog.dismiss();
                progressDialog = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

自定义OkGo回调类

/**
 * 自定义的OKGo回调类(添加接口请求时加载对话框)
 */
public abstract class StringDialogCallback extends StringCallback {

    private Context context;
    private boolean isShowDialog;//是否显示加载中对话框
    private LoadingUtil loadingUtil;//
    private String message;//加载提示语


    /**
     * @param context      上下文
     * @param isShowDialog 是否显示加载对话框false:不显示,true显示
     * @param message      自定义加载提示语
     */
    public StringDialogCallback(Context context, boolean isShowDialog, String message) {
        this.isShowDialog = isShowDialog;
        if (isShowDialog == true) {
            this.context=context;
            this.message=message;
            loadingUtil=new LoadingUtil();
        }
    }

    @Override
    public void onStart(Request<String, ? extends Request> request) {
        if (isShowDialog ==true) {
            loadingUtil.showProgressDialog(context,message);
        }
    }

    @Override
    public void onFinish() {
        if (isShowDialog ==true) {
            loadingUtil.dismissProgressDialog();
        }
    }
}

使用OkGo

public class MainActivity extends AppCompatActivity {

    private String url = "http://wxpay.wxutil.com/pub_v2/app/app_pay.php";//微信支付测试接口

    private Button bt_request;
    private TextView tv_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt_request = (Button) findViewById(R.id.bt_request);
        tv_content = (TextView) findViewById(R.id.tv_content);
        bt_request.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
            }
        });

    }

    /**
     * 请求数据
     */
    private void getData() {
        OkGo.<String>get(url)
//                .headers("token", "9790c789ac168cf7a6ef7ffe7168f542")//传参用
//                .params("name", "haha")//传参用
//                .params("page", 1)
//                .params("pageSize", 1)
                .tag(this)
                .execute(new StringDialogCallback(MainActivity.this, true, "加载数据中...") {
                    @Override
                    public void onSuccess(Response<String> response) {
                        tv_content.setText(response.body());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        Toast.makeText(getApplicationContext(), "接口请求错误!", Toast.LENGTH_LONG).show();
                    }

                });

    }


}

布局文件很简单就不粘出来了,送上源码下载地址:http://download.csdn.net/download/qq_29370483/10245948



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值