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();
        }
  • 相关阅读:
    昨晚值班将发dla的程序改好后放入正式环境
    本来今天打算不带电脑去值班
    有时候你会觉得,你的不真实让人怀疑,自己却非常尴尬
    其实对于公司的事情分布,我是昨天没有干什么
    异常处理
    反射及内置方法
    绑定方法与非绑定方法
    面向对象之多态
    面向对象之封装
    面向对象三大特性
  • 原文地址:https://www.cnblogs.com/gongcb/p/2520881.html
Copyright © 2011-2022 走看看