Search in sources :

Example 21 with WebResourceError

use of android.webkit.WebResourceError in project android-sdk by testpress.

the class WebViewUtils method initWebViewAndPostUrl.

@SuppressLint({ "AddJavascriptInterface", "SetJavaScriptEnabled" })
public void initWebViewAndPostUrl(String url, String json, final Activity activity) {
    initWebView(webView);
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            WebViewUtils.this.onPageStarted();
            hasError = false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (!hasError) {
                onLoadFinished();
            }
        }

        @SuppressWarnings("deprecation")
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return WebViewUtils.this.shouldOverrideUrlLoading(activity, url);
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            if (InternetConnectivityChecker.isConnected(activity)) {
                return;
            }
            onNetworkError();
            hasError = true;
        }
    });
    postUrl(url, json);
}
Also used : Bitmap(android.graphics.Bitmap) WebResourceRequest(android.webkit.WebResourceRequest) WebResourceError(android.webkit.WebResourceError) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient) SuppressLint(android.annotation.SuppressLint)

Example 22 with WebResourceError

use of android.webkit.WebResourceError in project SwipeToLoadLayout by Aspsine.

the class TwitterWebViewFragment method onViewCreated.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    swipeToLoadLayout = (SwipeToLoadLayout) view.findViewById(R.id.swipeToLoadLayout);
    webView = (WebView) view.findViewById(R.id.swipe_target);
    swipeToLoadLayout.setOnRefreshListener(this);
    swipeToLoadLayout.setOnLoadMoreListener(this);
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            swipeToLoadLayout.setRefreshing(false);
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            swipeToLoadLayout.setRefreshing(false);
        }

        @Override
        public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
            super.onReceivedHttpError(view, request, errorResponse);
            swipeToLoadLayout.setRefreshing(false);
        }
    });
}
Also used : WebResourceRequest(android.webkit.WebResourceRequest) WebResourceResponse(android.webkit.WebResourceResponse) WebResourceError(android.webkit.WebResourceError) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient)

Example 23 with WebResourceError

use of android.webkit.WebResourceError in project IbookerEditorAndroid by zrunker.

the class IbookerEditorWebView method init.

// 初始化
@SuppressLint({ "AddJavascriptInterface", "SetJavaScriptEnabled" })
private void init() {
    webSettings = this.getSettings();
    // 支持内容重新布局
    webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    // 允许JS
    webSettings.setJavaScriptEnabled(true);
    // 支持插件
    webSettings.setPluginState(WebSettings.PluginState.ON);
    // 设置允许JS弹窗
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    // access Assets and resources
    webSettings.setAllowFileAccess(true);
    webSettings.setAppCacheEnabled(false);
    // 提高渲染优先级
    webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
    // 设置编码格式
    webSettings.setDefaultTextEncodingName("utf-8");
    // 支持自动加载图片
    webSettings.setLoadsImagesAutomatically(true);
    // 关闭webview中缓存
    webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    // 将图片调整到适合webview的大小
    webSettings.setUseWideViewPort(true);
    // 缩放至屏幕的大小
    webSettings.setLoadWithOverviewMode(true);
    // 支持缩放,默认为true。
    webSettings.setSupportZoom(true);
    // 设置内置的缩放控件。若为false,则该WebView不可缩放
    webSettings.setBuiltInZoomControls(true);
    // 隐藏原生的缩放控件
    webSettings.setDisplayZoomControls(false);
    // 获取当前字体大小
    if (webSettings.getTextSize() == WebSettings.TextSize.SMALLEST) {
        currentFontSize = 1;
    } else if (webSettings.getTextSize() == WebSettings.TextSize.SMALLER) {
        currentFontSize = 2;
    } else if (webSettings.getTextSize() == WebSettings.TextSize.NORMAL) {
        currentFontSize = 3;
    } else if (webSettings.getTextSize() == WebSettings.TextSize.LARGER) {
        currentFontSize = 4;
    } else if (webSettings.getTextSize() == WebSettings.TextSize.LARGEST) {
        currentFontSize = 5;
    }
    // 隐藏滚动条
    setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    // 使页面获取焦点,防止点击无响应
    requestFocus();
    // 设置WebViewClient
    setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (ibookerEditorWebViewUrlLoadingListener != null)
                return ibookerEditorWebViewUrlLoadingListener.shouldOverrideUrlLoading(view, url);
            else {
                // view.loadUrl(url, additionalHttpHeaders);
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                getContext().startActivity(intent);
                return true;
            }
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            if (ibookerEditorWebViewUrlLoadingListener != null)
                return ibookerEditorWebViewUrlLoadingListener.shouldOverrideUrlLoading(view, request);
            else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    // view.loadUrl(request.getUrl().toString(), additionalHttpHeaders);
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(request.getUrl().toString()));
                    getContext().startActivity(intent);
                }
                return true;
            }
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            if (ibookerEditorWebViewUrlLoadingListener != null)
                ibookerEditorWebViewUrlLoadingListener.onReceivedError(view, request, error);
            else
                // 当网页加载出错时,加载本地错误文件
                IbookerEditorWebView.this.loadUrl("file:///android_asset/error.html", additionalHttpHeaders);
            isLoadError = true;
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            if (error.getPrimaryError() == SslError.SSL_DATE_INVALID || error.getPrimaryError() == SslError.SSL_EXPIRED || error.getPrimaryError() == SslError.SSL_INVALID || error.getPrimaryError() == SslError.SSL_UNTRUSTED) {
                handler.proceed();
            } else {
                handler.cancel();
            }
            if (ibookerEditorWebViewUrlLoadingListener != null)
                ibookerEditorWebViewUrlLoadingListener.onReceivedSslError(view, handler, error);
            else
                // 当网页加载出错时,加载本地错误文件
                IbookerEditorWebView.this.loadUrl("file:///android_asset/error.html", additionalHttpHeaders);
            isLoadError = true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            if (ibookerEditorWebViewUrlLoadingListener != null)
                ibookerEditorWebViewUrlLoadingListener.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            isLoadFinished = true;
            if (isExecuteCompile) {
                ibookerCompile(ibookerEditorText);
            } else if (isExecuteHtmlCompile) {
                ibookerHtmlCompile(ibookerEditorHtml);
            } else {
                addWebViewListener();
            }
            if (ibookerEditorWebViewUrlLoadingListener != null)
                ibookerEditorWebViewUrlLoadingListener.onPageFinished(view, url);
        }
    });
    // 添加js
    ibookerEditorJsCheckImgEvent = new IbookerEditorJsCheckImgEvent();
    addJavascriptInterface(ibookerEditorJsCheckImgEvent, "ibookerEditorJsCheckImgEvent");
    // 加载本地HTML
    loadUrl("file:///android_asset/ibooker_editor_index.html", additionalHttpHeaders);
    isLoadError = false;
}
Also used : SslErrorHandler(android.webkit.SslErrorHandler) Bitmap(android.graphics.Bitmap) WebResourceRequest(android.webkit.WebResourceRequest) SslError(android.net.http.SslError) WebResourceError(android.webkit.WebResourceError) Intent(android.content.Intent) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient) SuppressLint(android.annotation.SuppressLint)

Example 24 with WebResourceError

use of android.webkit.WebResourceError in project Palm300Heroes by nicolite.

the class WebViewActivity method initWebView.

private void initWebView() {
    settings = webView.getSettings();
    settings.setLoadWithOverviewMode(true);
    // 使页面支持缩放
    settings.setBuiltInZoomControls(true);
    // 支持缩放
    settings.setSupportZoom(true);
    // 缩放按钮
    settings.setDisplayZoomControls(false);
    // 支持自动加载图片
    settings.setLoadsImagesAutomatically(true);
    // 开启DOM
    settings.setDomStorageEnabled(true);
    // 关闭webview中缓存
    settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    settings.setAppCacheEnabled(true);
    settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    settings.setDefaultTextEncodingName("UTF-8");
    // 将图片调整到适合webview的大小
    settings.setUseWideViewPort(true);
    // //当webview调用requestFocus时为webview设置节点
    settings.setNeedInitialFocus(true);
    // 允许访问文件
    settings.setAllowFileAccess(true);
    // 支持缩放
    settings.setSupportZoom(true);
    settings.setPluginState(WebSettings.PluginState.ON);
    webView.setFocusable(true);
    webView.setFocusableInTouchMode(true);
    webView.addJavascriptInterface(new JsInject(this), "imageListener");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
    }
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String s) {
            if (s.startsWith("http") || s.startsWith("https")) {
                webView.loadUrl(s);
            } else if (s.startsWith("intent")) {
                try {
                    Intent intent = Intent.parseUri(s, Intent.URI_INTENT_SCHEME);
                    intent.addCategory("android.intent.category.BROWSABLE");
                    intent.setComponent(null);
                    intent.setSelector(null);
                    List<ResolveInfo> resolves = context.getPackageManager().queryIntentActivities(intent, 0);
                    if (resolves.size() > 0) {
                        startActivityIfNeeded(intent, -1);
                    }
                    return true;
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(s));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return true;
            }
            return super.shouldOverrideUrlLoading(webView, s);
        }

        @Override
        public void onPageStarted(WebView webView, String s, Bitmap bitmap) {
            super.onPageStarted(webView, s, bitmap);
            progressBar.setProgress(20);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (view != null && toolbarTitle != null) {
                String title = view.getTitle();
                if (!TextUtils.isEmpty(title) && !title.contains("about:blank")) {
                    toolbarTitle.setText(title);
                }
            }
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
        }
    });
    webView.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView webView, int i) {
            super.onProgressChanged(webView, i);
            if (progressBar != null) {
                if (i == 100) {
                    progressBar.setVisibility(View.GONE);
                    if (type != TYPE_VIDEO && type != TYPE_TY_CARTOON && type != TYPE_THEME_MUSIC) {
                        addImgClickListener();
                    }
                    if (type == TYPE_THEME_MUSIC) {
                        changThemeMusicDom();
                    }
                } else {
                    if (View.GONE == progressBar.getVisibility()) {
                        progressBar.setVisibility(View.VISIBLE);
                    }
                    if (i <= 20) {
                        progressBar.setProgress(20);
                    } else {
                        progressBar.setProgress(i);
                    }
                }
            }
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            if (!TextUtils.isEmpty(title) && !title.contains("about:blank")) {
                toolbarTitle.setText(title);
            }
        }
    });
}
Also used : WebResourceRequest(android.webkit.WebResourceRequest) Intent(android.content.Intent) URISyntaxException(java.net.URISyntaxException) URISyntaxException(java.net.URISyntaxException) JsInject(cn.nicolite.palm300heroes.injection.JsInject) ResolveInfo(android.content.pm.ResolveInfo) Bitmap(android.graphics.Bitmap) WebChromeClient(android.webkit.WebChromeClient) WebResourceError(android.webkit.WebResourceError) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient)

Example 25 with WebResourceError

use of android.webkit.WebResourceError in project IbookerEditorAndroid by zrunker.

the class IbookerEditorPreView method init.

// 初始化
@SuppressLint({ "SetJavaScriptEnabled", "AddJavascriptInterface" })
private void init() {
    WebSettings webSettings = this.getSettings();
    // 允许JS
    webSettings.setJavaScriptEnabled(true);
    // 设置允许JS弹窗
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    // access Assets and resources
    webSettings.setAllowFileAccess(true);
    webSettings.setAppCacheEnabled(false);
    // 提高渲染优先级
    webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
    // 设置编码格式
    webSettings.setDefaultTextEncodingName("utf-8");
    // 支持自动加载图片
    webSettings.setLoadsImagesAutomatically(true);
    // 关闭webview中缓存
    webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    // 将图片调整到适合webview的大小
    webSettings.setUseWideViewPort(true);
    // 缩放至屏幕的大小
    webSettings.setLoadWithOverviewMode(true);
    // 支持缩放,默认为true。
    webSettings.setSupportZoom(true);
    // 设置内置的缩放控件。若为false,则该WebView不可缩放
    webSettings.setBuiltInZoomControls(true);
    // 隐藏原生的缩放控件
    webSettings.setDisplayZoomControls(false);
    // 隐藏滚动条
    this.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    // 使页面获取焦点,防止点击无响应
    this.requestFocus();
    // 设置WebViewClient
    this.setWebViewClient(new WebViewClient() {

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            // 当网页加载出错时,加载本地错误文件
            IbookerEditorPreView.this.loadUrl("file:///android_asset/error.html");
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            addWebViewListener();
        }
    });
    // 添加js
    ibookerEditorJsCheckImgEvent = new IbookerEditorJsCheckImgEvent();
    this.addJavascriptInterface(ibookerEditorJsCheckImgEvent, "ibookerEditorJsCheckImgEvent");
    // 加载本地HTML
    this.loadUrl("file:///android_asset/ibooker_editor_index.html");
}
Also used : WebResourceRequest(android.webkit.WebResourceRequest) WebSettings(android.webkit.WebSettings) WebResourceError(android.webkit.WebResourceError) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient) SuppressLint(android.annotation.SuppressLint)

Aggregations

WebResourceError (android.webkit.WebResourceError)36 WebResourceRequest (android.webkit.WebResourceRequest)36 WebView (android.webkit.WebView)36 WebViewClient (android.webkit.WebViewClient)35 Bitmap (android.graphics.Bitmap)22 SuppressLint (android.annotation.SuppressLint)16 WebChromeClient (android.webkit.WebChromeClient)12 WebSettings (android.webkit.WebSettings)12 WebResourceResponse (android.webkit.WebResourceResponse)10 SslError (android.net.http.SslError)9 SslErrorHandler (android.webkit.SslErrorHandler)9 TargetApi (android.annotation.TargetApi)8 View (android.view.View)8 Intent (android.content.Intent)7 Uri (android.net.Uri)7 TextView (android.widget.TextView)6 Nullable (androidx.annotation.Nullable)4 ConsoleMessage (android.webkit.ConsoleMessage)3 Context (android.content.Context)2 ColorStateList (android.content.res.ColorStateList)2