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();
        }
  • 相关阅读:
    第36课 经典问题解析三
    第35课 函数对象分析
    67. Add Binary
    66. Plus One
    58. Length of Last Word
    53. Maximum Subarray
    38. Count and Say
    35. Search Insert Position
    28. Implement strStr()
    27. Remove Element
  • 原文地址:https://www.cnblogs.com/gongcb/p/2520881.html
Copyright © 2011-2022 走看看