ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码
当前位置:首页 >> 低调看体育直播 >> CSSjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播 >> Android 拦截WebView加载URL,控制其加载CSS、JS资源

Android 拦截WebView加载URL,控制其加载CSS、JS资源(2/3)

来源:网络整理     时间:2015-10-30     关键词:WebView,Exception,开发经验,请求方式,html代码

本篇文章主要介绍了" Android 拦截WebView加载URL,控制其加载CSS、JS资源",主要涉及到WebView,Exception,开发经验,请求方式,html代码方面的内容,对于CSSjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下: 绪论最近在项目中有了这样一个需求,我们都知道WebView加载网页可以缓存,但是web端想让客服端根据需求来缓存网页,也就是说web端在设置了http响应头,我...

class MyWebClient extends WebViewClient {
        @OverridepublicbooleanshouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            returntrue;
        }

        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Overridepublic WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
            if (!"get".equals(request.getMethod().toLowerCase())) {
                returnsuper.shouldInterceptRequest(view, request);
            }
            String url = request.getUrl().toString();

            //todo:计算url的hash
            String md5URL = YUtils.md5(url);

            //读取缓存的html页面
            File file = new File(appCacheDir + File.separator + md5URL);
            if (file.exists()) {
                FileInputStream fileInputStream = null;
                try {
                    fileInputStream = new FileInputStream(file);
                    Log.e(">>>>>>>>>", "读缓存");
                    returnnew WebResourceResponse(YUtils.readBlock(fileInputStream), YUtils.readBlock(fileInputStream), fileInputStream);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                returnnull;

            }

            try {
                URL uri = new URL(url);
                URLConnection connection = uri.openConnection();
                InputStream uristream = connection.getInputStream();
                String cache = connection.getHeaderField("Ddbuild-Cache");
                String contentType = connection.getContentType();
                //text/html; charset=utf-8
                String mimeType = "";
                String encoding = "";
                if (contentType != null && !"".equals(contentType)) {
                    if (contentType.indexOf(";") != -1) {
                        String[] args = contentType.split(";");
                        mimeType = args[0];
                        String[] args2 = args[1].trim().split("=");
                        if (args.length == 2 && args2[0].trim().toLowerCase().equals("charset")) {
                            encoding = args2[1].trim();
                        } else {

                            encoding = "utf-8";
                        }
                    } else {
                        mimeType = contentType;
                        encoding = "utf-8";
                    }
                }

                if ("1".equals(cache)) {
                    //todo:缓存uristream
                    FileOutputStream output = new FileOutputStream(file);
                    int read_len;
                    byte[] buffer = newbyte[1024];

                    YUtils.writeBlock(output, mimeType);
                    YUtils.writeBlock(output, encoding);
                    while ((read_len = uristream.read(buffer)) > 0) {
                        output.write(buffer, 0, read_len);
                    }
                    output.close();
                    uristream.close();

                    FileInputStream fileInputStream = new FileInputStream(file);
                    YUtils.readBlock(fileInputStream);
                    YUtils.readBlock(fileInputStream);
                    Log.e(">>>>>>>>>", "读缓存");
                    returnnew WebResourceResponse(mimeType, encoding, fileInputStream);
                } else {
                    Log.e(">>>>>>>>>", "网络加载");
                    returnnew WebResourceResponse(mimeType, encoding, uristream);
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            returnnull;
        }
       }
  //这里面读写操作比较多,还有截取那两个属性的字符串稍微有点麻烦/**
     * int转byte
     * by黄海杰 at:2015-10-29 16:15:06
     * @param iSource
     * @param iArrayLen
     * @return
     */publicstaticbyte[] toByteArray(int iSource, int iArrayLen) {
        byte[] bLocalArr = newbyte[iArrayLen];
        for (int i = 0; (i < 4) && (i < iArrayLen); i++) {
            bLocalArr[i] = (byte) (iSource >> 8 * i & 0xFF);
        }
        return bLocalArr;
    }

    /**
     * byte转int
     * by黄海杰 at:2015-10-29 16:14:37
     * @param bRefArr
     * @return
     */// 将byte数组bRefArr转为一个整数,字节数组的低位是整型的低字节位publicstaticinttoInt(byte[] bRefArr) {
        int iOutcome = 0;
        byte bLoop;

        for (int i = 0; i < bRefArr.length; i++) {
            bLoop = bRefArr[i];
            iOutcome += (bLoop & 0xFF) << (8 * i);
        }
        return iOutcome;
    }

    /**
     * 写入JS相关文件
     * by黄海杰 at:2015-10-29 16:14:01
     * @param output
     * @param str
     */publicstaticvoidwriteBlock(OutputStream output, String str) {
        try {
            byte[] buffer = str.getBytes("utf-8");
            int len = buffer.length;
            byte[] len_buffer = toByteArray(len, 4);
            output.write(len_buffer);
            output.write(buffer);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 读取JS相关文件
     * by黄海杰 at:2015-10-29 16:14:19
     * @param input
     * @return
     */publicstatic String readBlock(InputStream input) {
        try {
            byte[] len_buffer = newbyte[4];
            input.read(len_buffer);
            int len = toInt(len_buffer);
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            int read_len = 0;
            byte[] buffer = newbyte[len];
            while ((read_len = input.read(buffer)) > 0) {
                len -= read_len;
                output.write(buffer, 0, read_len);
                if (len <= 0) {
                    break;
                }
            }
            buffer = output.toByteArray();
            output.close();
            returnnew String(buffer,"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        returnnull;
    }
 //为了加密我们的html我们把url转成md5/**
     * 字符串转MD5
     * by黄海杰 at:2015-10-29 16:15:32
     * @param string
     * @return
     */publicstatic String md5(String string) {

        byte[] hash;

        try {

            hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));

        } catch (NoSuchAlgorithmException e) {

            thrownew RuntimeException("Huh, MD5 should be supported?", e);

        } catch (UnsupportedEncodingException e) {

            thrownew RuntimeException("Huh, UTF-8 should be supported?", e);

        }

        StringBuilder hex = new StringBuilder(hash.length * 2);

        for (byte b : hash) {

            if ((b & 0xFF) < 0x10) hex.append("0");

            hex.append(Integer.toHexString(b & 0xFF));

        }

        return hex.toString();

    }

注意

相关图片

相关文章