«

Android 接口的回调函数

时间:2024-3-2 17:45     作者:韩俊     分类: Android


实例代码:

(new OnHttpListener() {

            @Override
            public void onStart() {
                // TODO Auto-generated method stub
                super.onStart();
            }

            @Override
            public void onProgress(int mProgress, String message) {
                // TODO Auto-generated method stub
                super.onProgress(mProgress, message);
            }

            @Override
            public void onSuccess(boolean status, int resultCode, String message,
                    String data) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onFail(Exception e, String message) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onFinish() {
                // TODO Auto-generated method stub
                super.onFinish();
            }

        });

开发中经常遇到这类代码,看上去很神秘,onSuccess(),onFail 两个方法重载,其他方法需要就自己调用,其实很简单,不废话,直接上代码:

/**
 * 流程控制开始和结束回调函数
 * @author LanYan
 *
 */
public abstract interface OnProcessListener extends BaseListener {
    /**
     * 开始任务
     */
    void onStart();
    /**
     * 任务结束
     */
    void onFinish();
}

进度控制:

/**
 * 进度控制
 * @author LanYan
 *
 */
public abstract class OnProgressListener implements OnProcessListener{
    /**
     * 进度条的显示控制
     * @param mProgress
     * @param message
     */
    public abstract void onProgress(int mProgress,String message);

    @Override
    public void onStart() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFinish() {
        // TODO Auto-generated method stub

    }

Htt请求:

/**
 * HTTP访问进度控制
 * @author LanYan
 *
 */
public abstract class OnHttpListener extends OnProgressListener{

    @Override
    public void onProgress(int mProgress, String message) {
        // TODO Auto-generated method stub

    }

    /**
     * 请求失败回调函数,返回异常信息和状态码
     * @param e
     * @param message
     */
    public abstract void onFail(Exception e,String message);

    /**
     * 请求成功回调函数,返回响应数据
     * @param status
     * @param message
     * @param data
     * @param resultCode
     */
    public abstract void onSuccess(boolean status,int resultCode,String message,String data);
}

测试类:

public class Test  {
    public static void main(String[] args) {
        getHttpData(HttpType.post, url, parmas, new OnHttpListener() {

            /*@Override
            public void onStart() {
                // TODO Auto-generated method stub
                super.onStart();
            }*/
            @Override
            public void onSuccess(boolean status, int resultCode, String message,
                    String data) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onFail(Exception e, String message) {
                // TODO Auto-generated method stub

            }

            /*@Override
            public void onFinish() {
                // TODO Auto-generated method stub
                super.onFinish();
            }*/
        });
    }
    public static void getHttpData(String type,String url,NameValuePair parmas,OnHttpListener listener){

    }
    public enum HttpType{
        get,post,defult
    }
}



标签: android

热门推荐