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();
        }
  • 相关阅读:
    JDK1.8十个新特性
    问题:No more handles [Could not detect registered XULRunner to use]
    在未标记为正在运行时,调用了RunScript
    问题: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
    常用数据库的JDBC 的URL形式
    Myeclipse插件的三种安装方式
    mysqlError: Illegal connection port value '3306>]'
    异常:Bad version number in .class file
    安装msi文件遇到错误code=2502 or 2503 win8
    spring使用RedisTemplate的坑Could not get a resource from the pool
  • 原文地址:https://www.cnblogs.com/gongcb/p/2520881.html
Copyright © 2011-2022 走看看