使用nohttp getpost请求3

package com.xjerp.xjtech.nohttp;

import com.yanzhenjie.nohttp.rest.Response;

/**
 * Created by ${zpz} on 2018/3/29.
 */

public interface HttpListener <T> {
    //请求成功时会掉的监听方法
    void onSucceed(int what, Response<String> response);
    //请求网络失败回调的监听方法
    void onFailed(int what, Response<String> response);

}

package com.xjerp.xjtech.nohttp;

import android.content.Context;
import android.content.DialogInterface;

import com.yanzhenjie.nohttp.rest.OnResponseListener;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

/**
 * Created by ${zpz} on 2018/3/29.
 */

public class HttpResponseListener<T> implements OnResponseListener {
    //使用之前自定义的进度条对话框
    private WaitDialog mWaitDialog;
    //之前定义的监听方法的接口类
    private HttpListener<T> callback;
    //一个标识,看是否加载
    private boolean isLoading;
    //一个NoHttp队列
    private Request<?> mRequest;
    //上下文
    private Context context;
    //一个标识,判断进度条对话框是否可以取消
    private boolean canCancle;

    /**
     * @param callback  自定义的接口对象,就是复写Nohttp成功失败的那个类
     * @param isLoading 判断进度条对话框是否可以取消
     * @param request   Nohttp的队列对象
     * @param context   上下文
     * @param canCancle
     */
    public HttpResponseListener(Context context, Request<?> request, HttpListener<T> callback, boolean isLoading, boolean canCancle) {
        this.callback = callback;
        this.isLoading = isLoading;
        mRequest = request;
        this.context = context;
        this.canCancle = canCancle;
        if (context != null && isLoading) {
            mWaitDialog = new WaitDialog(context);
            mWaitDialog.setCancelable(canCancle);
            //设置监听器
            //当对话框点击外面可以取消,那么就让他取消的逻辑代码
            mWaitDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    HttpResponseListener.this.mRequest.cancel();
                }
            });
        }
    }

    //请求网络开始时回调,进度条对话框的显示
    @Override
    public void onStart(int what) {
        //判断进度条对话框是否处于夹在状态,进度条对话框对象是否存在,进度条对话框是否存在
        if (isLoading && mWaitDialog != null && !mWaitDialog.isShowing()) {
            //显示对话框
//            mWaitDialog.show();
        }
    }

    //网络请求成功时回调,代码直接运行在主线程
    @Override
    public void onSucceed(int what, Response response) {
        //使用的是自己定义的接口,先判断接口对象是否为Null,不为Null时执行
        if (callback != null) {
            callback.onSucceed(what, response);
        }
    }

    //网络请求失败时回调,代码直接运行在主线程
    @Override
    public void onFailed(int what, Response response) {


        if (callback != null)
            callback.onFailed(what, response);
        //使用的是自己定义的接口,先判断接口对象是否为Null,Null时执行
        if (callback != null) {
            callback.onFailed(what, response);
        }
    }

    //请求结束时进度条对话框隐藏
    @Override
    public void onFinish(int what) {
        //判断进度条对话框是否处于夹在状态,进度条对话框对象是否存在,进度条对话框是否存在
        if (isLoading && mWaitDialog != null && mWaitDialog.isShowing()) {
            //隐藏对话框
            mWaitDialog.dismiss();
        }
    }
}


public class WaitDialog extends ProgressDialog {
    public WaitDialog(Context context) {
        super(context);
        //设置当前的ActivityTitle,并且全屏(点用这个方法必须在setContentView之前)
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置对话框的外面点击,是否让对话框消失,false是可以取消
        setCanceledOnTouchOutside(false);
        //设置对话框的样式
        setProgressStyle(STYLE_SPINNER);
        //设置进度条显示的内容
        setMessage("正在请求,请稍候");
    }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值