最终调用时的代码

    private void ansyClearApplyInfor() {
        RequestParams params = new RequestParams();
        params.put("uid", AppUser.getInstance().getUser().getuId());
        params.put("session_id", AppUser.getInstance().getUser().getuSessionId());
        params.put("family_id", AppUser.getInstance().getUser().getUserFamily().getId());//申请加入的帮会id
        DialogUtil.showProgressDialog(this, "", "请求中...", true, null);  
        AsyncXiuHttpHelper.post(Constants.GROUP_APPLY_CLEAN, params, new OnHttpListener<JSONObject>() {
            @Override
            public void onHttpListener(boolean httpSuccessed, JSONObject obj) {
                DialogUtil.dismissProgressDialog();  
                Log.i("-bqt", "=====点击清空申请记录后返回的数据:" + obj.toString());
                if (httpSuccessed) {
                    if (obj.optInt("result", -1) == 1) {
                    } else {
                        ApplicationUtil.showToast(FamilyApplyManageActivity.this, obj.optString("msg", "失败"));
                    }
                } else {
                    ApplicationUtil.showToast(FamilyApplyManageActivity.this, "网络错误");
                }
            }
        });

}


公司封装的http请求帮助类

public class AsyncXiuHttpHelper {
    public static final String SERVER_URL = "api.95xiu.com";
    public static final String WEB_SERVER_URL = "chat.95xiu.com";
    public static final int LIVE_WEB_PORT = 3016;

    public static String getAbsoluteUrl(String relativeUrl) {
        return AsyncHttpHelper.getAbsoluteUrl(SERVER_URL, relativeUrl);
    }
    /**
     * 返回基础RequestParams
     */
    private static RequestParams formatRequestParams(RequestParams params) {
        if (params == null) params = new RequestParams();
        params.put("imei", Properties.IMEI);
        params.put("channel", Properties.CN);
        params.put("session_id", AppUser.getInstance().getUser().getuSessionId());
        params.put("version_code", Properties.VERSION_CODE);
        return params;
    }
    /**
     * get 请求
     */
    public static void get(final String relativeUrl, RequestParams params, final OnHttpListener<JSONObject> onHttpListner) {
        params = formatRequestParams(params);
        AsyncHttpHelper.get(SERVER_URL, relativeUrl, params, onHttpListner);
    }
    /**
     * post 请求
     */
    public static void post(final String relativeUrl, RequestParams params, final OnHttpListener<JSONObject> onHttpListner) {
        params = formatRequestParams(params);
        AsyncHttpHelper.post(SERVER_URL, relativeUrl, params, onHttpListner);
    }

}


基础的http请求帮助类


public class AsyncHttpHelper {
    private static final int HTTP_OK = 200;
    private static final String HTTP_ERROR = "系统繁忙,请稍后再试";
    /**
     * 生成网络错误的json
     */
    private static JSONObject getErrorJson(String error) {
        JSONObject obj = new JSONObject();
        try {
            obj.put("error", error);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return obj;
    }
    /**
     * 拼接成完整 http url
     */
    public static String getAbsoluteUrl(String serverName, String relativeUrl) {
        return ("http://" + serverName + relativeUrl);
    }
    /**
     * 返回基础RequestParams
     */
    public static RequestParams formatRequestParams(RequestParams params) {
        if (params == null) params = new RequestParams();
        return params;
    }
    /**
     * 添加ky-value Requestparams
     */
    public static void addRequestParam(RequestParams params, String key, String value) {
        if (params == null) params = new RequestParams();
        params.add(key, value);
    }
    /**
     * get 请求,返回json
     */
    public static void get(final String serverName, final String relativeUrl, RequestParams params, final OnHttpListener<JSONObject> onHttpListner) {
        params = formatRequestParams(params);
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.setTimeout(10000);
        try {
            asyncHttpClient.get(getAbsoluteUrl(serverName, relativeUrl), params, new JsonHttpResponseHandler("UTF-8") {
                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    super.onFailure(statusCode, headers, responseString, throwable);
                }
                @Override
                public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                    throwable.printStackTrace();
                    if (onHttpListner != null) onHttpListner.onHttpListener(false, getErrorJson(HTTP_ERROR));
                }
                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
                    super.onSuccess(statusCode, headers, response);
                }
                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                    if (onHttpListner != null) {
                        if (statusCode == HTTP_OK) {
                            response = (response == null ? (new JSONObject()) : response);
                            onHttpListner.onHttpListener(true, response);
                        } else onHttpListner.onHttpListener(false, response);
                    }
                }
                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    super.onSuccess(statusCode, headers, responseString);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            if (onHttpListner != null) onHttpListner.onHttpListener(false, getErrorJson(HTTP_ERROR));
        }
    }
    /**
     * post 请求,返回json
     */
    public static void post(final String serverName, final String relativeUrl, RequestParams params, final OnHttpListener<JSONObject> onHttpListner) {
        params = formatRequestParams(params);
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.setTimeout(10000);
        try {
            String mm = getAbsoluteUrl(serverName, relativeUrl);
            asyncHttpClient.post(mm, params, new JsonHttpResponseHandler("UTF-8") {
                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    super.onFailure(statusCode, headers, responseString, throwable);
                }
                @Override
                public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
                    super.onFailure(statusCode, headers, throwable, errorResponse);
                }
                @Override
                public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                    throwable.printStackTrace();
                    if (onHttpListner != null) onHttpListner.onHttpListener(false, getErrorJson(HTTP_ERROR));
                }
                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
                    super.onSuccess(statusCode, headers, response);
                }
                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                    if (onHttpListner != null) {
                        if (statusCode == HTTP_OK) {
                            response = (response == null ? (new JSONObject()) : response);
                            onHttpListner.onHttpListener(true, response);
                        } else onHttpListner.onHttpListener(false, response);
                    }
                }
                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    super.onSuccess(statusCode, headers, responseString);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            if (onHttpListner != null) onHttpListner.onHttpListener(false, getErrorJson(HTTP_ERROR));
        }
    }
    /**
     * get 返回String
     */
    public static void get_AbsoluteUrl_String(final String url, RequestParams params, final OnHttpListener<String> onHttpListner) {
        params = formatRequestParams(params);
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.setTimeout(10000);
        try {
            asyncHttpClient.get(url, params, new AsyncHttpResponseHandler() {
                @Override
                public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
                    if (onHttpListner != null) onHttpListner.onHttpListener(false, arg2 == null ? "error" : new String(arg2));
                }
                @Override
                public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                    if (arg0 == HTTP_OK) {
                        if (onHttpListner != null) onHttpListner.onHttpListener(true, arg2 == null ? "error" : new String(arg2));
                    } else {
                        if (onHttpListner != null) onHttpListner.onHttpListener(false, arg2 == null ? "error" : new String(arg2));
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            if (onHttpListner != null) onHttpListner.onHttpListener(false, "");
        }
    }
    /**
     * get 返回JSONArray
     */
    public static void get_AbsoluteUrl_JSONArray(final String absoulteUrl, RequestParams params, final OnHttpListener<JSONArray> onHttpListner) {
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.setTimeout(10000);
        asyncHttpClient.get(absoulteUrl, params, new JsonHttpResponseHandler("UTF-8") {
            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
                super.onFailure(statusCode, headers, throwable, errorResponse);
            }
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
                if (onHttpListner != null) {
                    if (statusCode == HTTP_OK) {
                        response = (JSONArray) (response == null ? (new JSONObject()) : response);
                        onHttpListner.onHttpListener(true, response);
                    } else onHttpListner.onHttpListener(false, response);
                }
            }
        });
    }
    /**
     * post请求
     */
    public static void httpsPost(String url, RequestParams params, final OnHttpListener<String> onHttpListener) {
        params = formatRequestParams(params);
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        KeyStore trustStore = null;
        try {
            trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        } catch (KeyStoreException e1) {
            e1.printStackTrace();
        }
        try {
            trustStore.load(null, null);
        } catch (NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (CertificateException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        MySSLSocketFactory socketFactory = null;
        try {
            socketFactory = new MySSLSocketFactory(trustStore);
        } catch (KeyManagementException e1) {
            e1.printStackTrace();
        } catch (UnrecoverableKeyException e1) {
            e1.printStackTrace();
        } catch (NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (KeyStoreException e1) {
            e1.printStackTrace();
        }
        socketFactory.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        asyncHttpClient.setSSLSocketFactory(socketFactory);
        asyncHttpClient.setTimeout(10000);
        try {
            params = formatRequestParams(params);
            asyncHttpClient.post(url, params, new AsyncHttpResponseHandler() {
                @Override
                public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
                    if (onHttpListener != null) onHttpListener.onHttpListener(false, arg2 == null ? "error" : new String(arg2));
                }
                @Override
                public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                    if (arg0 == HTTP_OK) {
                        if (onHttpListener != null) onHttpListener.onHttpListener(true, arg2 == null ? "error" : new String(arg2));
                    } else {
                        if (onHttpListener != null) onHttpListener.onHttpListener(false, arg2 == null ? "error" : new String(arg2));
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            if (onHttpListener != null) onHttpListener.onHttpListener(false, "");
        }
    }
    /**
     * 设置持久化保存cookie
     */
    public static void saveCookie(Context context) {
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        PersistentCookieStore cookieStore = new PersistentCookieStore(context);
        asyncHttpClient.setCookieStore(cookieStore);
    }
    public static void can() {
    }
    /**********************************************************
     * 
     * Interface
     * 
     *********************************************************/
    public interface OnHttpListener<T> {
        public void onHttpListener(boolean httpSuccessed, T obj);
    }

}


95秀-异步http请求完整过程的更多相关文章

  1. HTTP深入浅出 http请求完整过程

    HTTP(HyperText Transfer Protocol)是一套计算机通过网络进行通信的规则.计算机专家设计出HTTP,使HTTP客户(如Web浏览器)能够从HTTP服务器(Web服务器)请求 ...

  2. 浏览器输入URL之后,HTTP请求返回的完整过程

    1.输入url,按下回车时,先做一个redirect(重定向),因为浏览器可能记录本机的地址已经永久跳转成新的地址,所以一开始浏览器就先要判断下需不需要重定向,以及重定向到哪里:2.然后第二步就是看A ...

  3. 一次HTTP请求服务的完整过程-请求处理过程

    0.DNS域名解析:递归查询.迭代查询 递归查询:客户端向第一个服务器查询,给最终结果 迭代查询:第一个服务器向根查询 1 .建立连接:接收或拒绝连接请求:三次握手的过程 提高HTTP 连接性能: 并 ...

  4. 字符型图片验证码识别完整过程及Python实现

    字符型图片验证码识别完整过程及Python实现 1   摘要 验证码是目前互联网上非常常见也是非常重要的一个事物,充当着很多系统的 防火墙 功能,但是随时OCR技术的发展,验证码暴露出来的安全问题也越 ...

  5. 转: 数字证书原理 https 完整过程解析

    点评: 讲的非常的详细与全面,值得一看. 转: http://www.cnblogs.com/JeffreySun/archive/2010/06/24/1627247.html 文中首先解释了加密解 ...

  6. HTTP请求响应过程 与HTTPS区别

    原文:HTTP请求响应过程 与HTTPS区别 HTTP协议学习笔记,基础,干货 HTTP协议 HTTP协议主要应用是在服务器和客户端之间,客户端接受超文本. 服务器按照一定规则,发送到客户端(一般是浏 ...

  7. Asp.Net请求响应过程

    Asp.Net请求响应过程 在之前,我们写了自己的Asp.Net框架,对整个流程有了一个大概的认识.这次我们来看一下Asp.Net整个请求处理过程是怎么样的. 浏览器封装请求报文,发送请求到达服务器, ...

  8. live555_RTSP连接建立以及请求消息处理过程

    1,RTSP连接的建立过程    RTSPServer类用于构建一个RTSP服务器,该类同时在其内部定义了一个RTSPClientSession类,用于处理单独的客户会话.    首先创建RTSP服务 ...

  9. Tomcat系列(6)——Tomcat处理一个HTTP请求的过程

    Tomcat的架构图   图三:Tomcat Server处理一个HTTP请求的过程 处理HTTP请求过程 假设来自客户的请求为:http://localhost:8080/test/index.js ...

随机推荐

  1. ubuntu系统mysql.h no such file or directory

    在Ubuntu系统中,你已经安装了mysql,即你使用sudo apt-get install mysql-server mysql-client然而使用C语言访问mysql数据库时,却发现出现了如下 ...

  2. Javascript模块化编程:模块的写法

    声明:本文转载自:阮一峰的网络日志,原文地址http://www.ruanyifeng.com/blog/2012/10/javascript_module.html,http://www.ruany ...

  3. UITextField AutoComplete iOS输入框内文本自动完成

    当你打开Safari的时候,输入网址,会有许多候选网址,点击后,自动填充到输入框,进入网页. 打开词典查单词的时候,输入前面部分字母,软件会给出符合的候选单词. 这样做的目的,是为了省去用户繁琐的输入 ...

  4. Win7下安装Ubuntu双系统常见问题

    [转自己以前的文章] 导航: ● win7下硬盘安装ubuntu常见问题 ●  win7下u盘安装ubuntu常见问题 吐槽: Ubuntu的安装花了我将近15个小时,网上常见的.不常见的问题基本我都 ...

  5. JQuery相关的网络资源

    jquery插件列表 国外网站:http://plugins.jquery.com/ 国内网站:http://www.oschina.net/project/tag/273/jquery

  6. Python环境配置及项目建立

    一.安装Python Python比较稳定的两个版本是Python 3.5和Python 2.7,我用的是Python 2.7,下载地址是:https://www.python.org/downloa ...

  7. Js监控回车事件

    标题通俗的说,也就是绑定当用户按下回车键要执行的事件. 下面,入正题. 第一步,先编写简单的页面代码,这里我们只需要一个按钮就足够了.当然,还有按钮事件. <html> <head& ...

  8. soket客户端程序(一)

    soket客户端主要完成以下步骤: 1.建立soket套接字(将套接字理解为一个通道) 2.建立连接 3.向服务器发送http请求 4.接收得到的数据 5.关闭连接 6.本地处理得到的数据 http: ...

  9. Java中抽象类和接口的用法和区别

    一.抽象类 1.抽象类 包含一个抽象方法的类就是抽象类 2.抽象方法 声明而未被实现的方法,抽象方法必须使用abstract关键词字声明 public abstract class People { ...

  10. Centos6.x 64位 安装JDK

    JDK下载地址: http://www.oracle.com/technetwork/cn/java/javase/downloads/jdk7-downloads-1880260-zhs.html ...