Http请求代码模板——Android

Http请求代码模板——Android,第1张

概述 依赖最下面的方法:   /**     * http访问网络     * @param url HTTP请求地址     * @param paramsMap 请求参数Map     * @param method HTTP请求方法:get或者post     * @param requestHeader 请求头参数Map     * @retur  依赖最下面的方法:
   /**     * http访问网络     * @param url http请求地址     * @param paramsMap 请求参数Map     * @param method http请求方法:get或者post     * @param requestheader 请求头参数Map     * @return     */    public static MaphttpInterface(String url, MapparamsMap, String method,Map requestheader){        httpGet get = null;//get访问        httpPost post = null;//post访问        httpResponse response = null;//响应\        //http结果map        MaphttpMap = new HashMap();        try {            httpMap.put("code", "succeed");            Log.i(TAG, "http请求地址: " + url);            //创建一个DefaulthttpClIEnt的实例            DefaulthttpClIEnt clIEnt = new DefaulthttpClIEnt();            if (url.indexOf("httpS:") > -1){                clIEnt = (DefaulthttpClIEnt) WebClIEntDevWrapper.wrapClIEnt(clIEnt);            }            //设置连接超时时间,单位毫秒            clIEnt.getParams().setParameter(CoreConnectionPnames.CONNECTION_TIMEOUT, 10000);            //等待结果超时时间,单位毫秒            clIEnt.getParams().setParameter(CoreConnectionPnames.so_TIMEOUT, 20000);            if (method.equalsIgnoreCase("GET")) {                //get访问                get = new httpGet(url);                //请求头参数不为空时,设置请求头                if(!QwyUtil.isNullAndEmpty(requestheader)){                    for(String key:requestheader.keySet()){                        get.setheader(key,requestheader.get(key));                    }                }                response = clIEnt.execute(get);            } else {                // Post访问;                post = new httpPost(url);                //请求头参数不为空时,设置请求头                if(!QwyUtil.isNullAndEmpty(requestheader)){                    for(String key:requestheader.keySet()){                        post.setheader(key,requestheader.get(key));                    }                }                //封装参数                if (!isNullAndEmpty(paramsMap)) {                    Listparams = new ArrayList();                    I@R_404_6704@torkeys = paramsMap.keySet().i@R_404_6704@tor();                    while (keys.hasNext()) {                        String key = keys.next();                        params.add(new BasicnameValuePair(key, paramsMap.get(key).toString()));                    }                    httpentity entity = new UrlEncodedFormEntity(params, "UTF-8");                    post.setEntity(entity);                }                response = clIEnt.execute(post);            }            if (response.getStatusline().getStatusCode() == httpStatus.SC_OK) {                String str = EntityUtils.toString(response.getEntity(), "UTF-8");                JsONObject JsonObject = new JsONObject(str);                return JsonToMap(JsonObject);            } else{                httpMap.put("code", "fail");                httpMap.put("message", "http访问异常:"+response.getStatusline().toString());                Log.e(TAG, response.getStatusline().toString());            }        } catch (Exception e) {            httpMap.put("code", "fail");            httpMap.put("message", "http访问异常!");            e.printstacktrace();            Log.e(TAG, "" + e.getMessage());        } finally {            return httpMap;        }    }    /**     * 将Json转换成Map格式;     * @param Json JsONObject     * @return     */    public static Map JsonToMap(JsONObject Json) {        Mapmap = new HashMap();        try {            if (!isNullAndEmpty(Json)) {                I@R_404_6704@tori@R_404_6704@tor = Json.keys();                while (i@R_404_6704@tor.hasNext()) {                    String key = i@R_404_6704@tor.next();                    map.put(key, Json.get(key));                }            }        } catch (JsONException e) {            e.printstacktrace();            Log.e(TAG, e.getMessage());        }        return map;    }
依赖此方法:
import org.apache.http.conn.scheme.Scheme;import org.apache.http.conn.scheme.SchemeRegistry;import org.apache.http.impl.clIEnt.DefaulthttpClIEnt;import org.apache.http.impl.conn.tsccm.ThreadSafeClIEntConnManager;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import javax.net.ssl.SSLContext;import javax.net.ssl.TrustManager;import javax.net.ssl.x509trustmanager;/** * 避免httpClIEnt的”SSLPeerUnverifIEdException: peer not authenticated”异常 * 不用导入SSL证书 * @author shipengzhi(shipengzhi@sogou-inc.com) * */public  class WebClIEntDevWrapper {    public static org.apache.http.clIEnt.httpClIEnt wrapClIEnt(org.apache.http.clIEnt.httpClIEnt base) {        try {            SSLContext ctx = SSLContext.getInstance("TLS");            x509trustmanager tm = new x509trustmanager() {                public X509Certificate[] getAcceptedissuers() {                    return null;                }                public voID checkClIEntTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}                public voID checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}            };            ctx.init(null, new TrustManager[] { tm }, null);                     SchemeRegistry registry = new SchemeRegistry();            registry.register(new Scheme("https", new EasySSLSocketFactory(), 443));            registry.register(new Scheme("https", new EasySSLSocketFactory(), 9543));            ThreadSafeClIEntConnManager mgr = new ThreadSafeClIEntConnManager(base.getParams(),registry);            return new DefaulthttpClIEnt(mgr, base.getParams());        } catch (Exception ex) {            ex.printstacktrace();            return null;        }    }}
总结

以上是内存溢出为你收集整理的Http请求代码模板——Android全部内容,希望文章能够帮你解决Http请求代码模板——Android所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1003805.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存