zoukankan      html  css  js  c++  java
  • Android Http 框架 LiteHTTP

    liteHttp初始化:

    public class LiteHttpManager {//
        protected static LiteHttp liteHttp;
    
        public static LiteHttp getInstance() {
            if (liteHttp == null) {
                initLiteHttp(App.mContext);
            }
            return liteHttp;
        }
    
        private static void initLiteHttp(Context context) {
            HttpConfig config = new HttpConfig(context) // configuration quickly
                    .setDebugged(true)// log output when debugged
                    .setDetectNetwork(true)              // detect network before connect
                    .setDoStatistics(true)
                    .setDefaultMaxRetryTimes(3)
                    .setDefaultCharSet("UTF-8")
                    .setDebugged(Utils.isDebug())
                    .setForRetry(3, false) // statistics of time and traffic
                    //.setDisableNetworkFlags(HttpConfig.FLAG_NET_DISABLE_MOBILE) //不用2/3/4G网络
                    .setUserAgent("Mozilla/5.0 (...)")   // set custom User-Agent
                    .setTimeOut(8000, 50000);             // connect and socket timeout: 2s
            liteHttp = LiteHttp.newApacheHttpClient(config);
        }
    
    
    }
    

      

    liteHttp 请求网络:

    public void requestInspectionPoolData(String time, String inspectiontype) {
            StringRequest postRequest = new StringRequest("url");
    
            LinkedList<NameValuePair> pList = new LinkedList<NameValuePair>();
            pList.add(new NameValuePair("User", "");
            pList.add(new NameValuePair("possword", "");
            postRequest.setHttpBody(new UrlEncodedFormBody(pList));
            postRequest.setMethod(HttpMethods.Post);
            postRequest.setHttpListener(new HttpListener<String>() {
                @Override
                public void onSuccess(String s, Response<String> response) {
                    super.onSuccess(s, response);
                    Log.i(TAG, "onSuccess: " + s);
                    //解析服务器返回数据
                }
    
                @Override
                public void onFailure(HttpException e, Response<String> response) {
                    super.onFailure(e, response);
                    //网络请求失败
                }
            });
         //异步访问 LiteHttpManager.getInstance().executeAsync(postRequest); }

      

    liteHttp 封装 HTTPBody:

      ApiParameter:

    /**
     * Created by Alen on 2017/2/11.
     * <p>
     * 请求接口的父类
     */
    public class ApiParameter {
    
        // 子类 如果要添加参数需要重写该方法, 否则默认为无 额外参数
        //
        public ApiParamMap buildExterParameter() {
            return new ApiParamMap();
        }
    
        // 子类, 如果需要添加sessionKey 需要覆盖此函数并且返回true
        public boolean needSessionKey() {
            return false;
        }
    
        public MultipartBody getRequestBody() {
            ApiParamMap apiParamMap = buildExterParameter();
            apiParamMap.put("os", new ApiParamMap.ParamData("android"));
            apiParamMap.put("appVersion", new ApiParamMap.ParamData(Utils.getVersion()));
            apiParamMap.put("versionCode", new ApiParamMap.ParamData(Utils.getVersionCode() + ""));
            apiParamMap.put("channel", new ApiParamMap.ParamData(Utils.getChannelName()));
            apiParamMap.put("osVersion", new ApiParamMap.ParamData(android.os.Build.VERSION.SDK_INT + ""));
    
            MultipartBody body = new MultipartBody();
            List<String> lists = new ArrayList<String>(apiParamMap.keySet());
            for (String key : lists) {
                if(apiParamMap.get(key).value == null) apiParamMap.get(key).value = "";//防止外面过来空对象  导致崩溃
                body.addPart(new StringPart(key, apiParamMap.get(key).value));
            }
            return body;
        }
    }
    

      

    ApiParamMap:

    public class ApiParamMap extends LinkedHashMap<String, ApiParamMap.ParamData> {
        public ApiParamMap() {
            super();
        }
    
        public static class ParamData {
            public String value;
    
            public ParamData(String value) {
                this.value = value;
            }
        }
    }
    

      

    ApiParameter 子类:

    public class ActionParmeter extends ApiParameter {
    
        private String user;
        private String password;

    //方法参数为调用URL需要的参数 public ActionParmeter(String user, String password) { this.user= user; this.password= password; }
      //重写父类方法 @Override public ApiParamMap buildExterParameter() { ApiParamMap map=new ApiParamMap(); map.put("user", new ApiParamMap.ParamData(this.user)); map.put("password", new ApiParamMap.ParamData(this.password)); return map; } }

      

    调用:

    stringRequest.setHttpBody(new ActionDetailsParmeter(user,password).getRequestBody());
    

     

    
    
  • 相关阅读:
    Discuz X 2.5 点点(伪静态)
    jq 、xml 省市级联动
    php memcache 初级使用(2)
    关于windows虚拟内存管理的页目录自映射
    SharePoint 2010 网络上的开发经验和资源
    SharePoint 2010 Reporting Services 报表服务器正在内置 NT AUTHORITY\SYSTEM 账户下运行 解决方法
    SharePoint 2010 Reporting Services 报表服务器无法解密用于访问报表服务器数据库中的敏感数据或加密数据的对称密钥 解决方法
    Active Directory Rights Management Services (AD RMS)无法检索证书层次结构。 解决方法
    SharePoint 2010 Reporting Services 报表服务器实例没有正确配置 解决方法
    SharePoint 2010 页面引用 Reporting Services 展现 List 报表
  • 原文地址:https://www.cnblogs.com/IT-lss/p/9565946.html
Copyright © 2011-2022 走看看