zoukankan      html  css  js  c++  java
  • Android进阶篇Http协议

    在Android开发过程中,很多情况下,我们需要通过Http协议获取服务端的数据接口。

    这里我们通过封装一个Http工具类:

        /**
         * 发送http请求
         * @param urlPath 请求路径
         * @param requestType 请求类型
         * @param request 请求参数,如果没有参数,则为null
         * 
         * @return
         */
        public static String sendRequest(String urlPath, String requestType, String request,String tokenid) {
            
            URL url = null;
            HttpURLConnection conn = null;
            OutputStream os = null;
            InputStream is = null;
            String result = null;
            try {
                url = new URL(urlPath);
                conn = (HttpURLConnection)url.openConnection();
                if (!"".equals(requestType)) {
                    conn.setRequestMethod(requestType);
                }
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setReadTimeout(10 * 1000);
                conn.setConnectTimeout(6 * 1000);
                conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
                conn.setRequestProperty("Connection", "keep-alive");
                conn.setRequestProperty("Content-Length", Integer.toString(request.trim().getBytes().length));
                
                if (!"".equals(tokenid)) {
                    conn.setRequestProperty("tokenid", tokenid);
                }
    
                if (request != null && !"".equals(request)) {
                    os = conn.getOutputStream();
                    os.write(request.getBytes());
                    os.flush();
                }
                Log.i(TAG,"code="+conn.getResponseCode());
                
                if (200 == conn.getResponseCode()) {
                    is = conn.getInputStream();
                    byte[] temp = readStream(is);
                    result = new String(temp);
                }
                
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (is != null) {
                        is.close();
                    }
                    if (os != null) {
                        os.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (result != null) {
                return result;
            } else {
                return null;
            }
        }
        
        public static byte[] readStream(InputStream is) throws Exception {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            byte[] buffer = new byte[2048];
            int len = 0;
            while((len = is.read(buffer)) != -1){
                os.write(buffer,0,len);
            }
            is.close();
            return os.toByteArray();
        }
  • 相关阅读:
    LeetCode算法题-Trim a Binary Search Tree(Java实现)
    LeetCode算法题-Non-decreasing Array(Java实现)
    LeetCode算法题-Image Smoother(Java实现)
    Node.js 官方文档中文版
    jade 网上看到一个不错的demo 分享 一下 链接
    jade 的 考古
    标题党 数据抓取与管理
    最近面试 有人问 sqlite 用过么 sqlite 不是 嵌入式的 开发 么 难道最近还 web开发 了?
    嗯 想写个demo 苦于没数据
    客户端 jQuery 跨端口 调用 node 服务端
  • 原文地址:https://www.cnblogs.com/gongcb/p/2520881.html
Copyright © 2011-2022 走看看