标签: webviewclient

加载WebView页面时如何为警告对话框设置不同的标题?

我想在WebView加载页面时为警报对话框设置不同的标题,但它不起作用.

这是代码片段:

final AlertDialog.Builder alert = new AlertDialog.Builder(
        mContext);
// alert.setTitle("Loading...");
final WebView wv = new WebView(mContext);

wv.loadUrl("http://10.0.51.133/androidview/");
wv.getSettings().setJavaScriptEnabled(true);
wv.setVerticalScrollBarEnabled(false);

WebViewClientLoader loader= new WebViewClientLoader(alert);
wv.setWebViewClient(loader);
wv.setWebViewClient(new WebViewClient() {

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        alert.setTitle("Loading...");
        super.onPageFinished(view, url);
    }

    @Override
    public void onPageStarted(WebView view, String url,
            Bitmap favicon) {
        // TODO Auto-generated method stub
        alert.setTitle("Finished");
        super.onPageStarted(view, url, favicon);
    }
});

private class webviewclient extends WebViewClient{

}

wv.loadUrl("file:///android_asset/Like.html");
alert.setView(wv);

alert.show();
Run Code Online (Sandbox Code Playgroud)

android webview webviewclient

6
推荐指数
2
解决办法
3454
查看次数

Android WebView:拦截重定向请求?

有什么方法可以拦截Android WebView中的重定向请求?shouldInterceptRequest(WebView,WebResourceRequest)似乎没有被调用!

我想通过以下方式自动捕获所有Web视图请求中的令牌到期:

  1. 从服务器发出重定向而不是401
  2. 在需要登录的地方使用shouldOverrideUrlLoading()调用AppAuth
  3. 在无需登录的情况下,使用shouldInterceptRequest通过刷新的令牌弹回原始URL

第2项工作正常,但第3项却失败了,因为似乎没有调用shouldInterceptRequest进行重定向,这确实是错误的-尤其是因为这没有记录,并且WebResourceRequest API会使人相信,甚至可以检查是否请求是重定向。

我会很乐意响应401而不是使用重定向-但我看不到有使用更新的令牌“就地”重试该请求的方法,除非该请求恰好是顶级页面请求。

我想我可以尝试老“页面”重定向,而不是302的,看看是否能工作的更好,但即使它是真正的黑客攻击。

(请注意,这显然不同于Android WebView,它是如何在应用程序中处理重定向而不是打开浏览器的问题 -因为我已经拥有一个Webview,并且正在尝试拦截和操纵重定向请求。)

redirect android webview webviewclient

6
推荐指数
1
解决办法
1593
查看次数

在Android WebViewClient中获取空白页面

我正在尝试打开twitter链接:http://mobile.twitter.com/pawan_rathore88 在我的活动中.如果我将WebViewClient设置为webview,我将获得空白页面.但是当我在没有设置任何webviewclient的情况下加载url时,它会正确加载页面.有谁知道什么可能是一个问题.以下是我的代码段.

webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
//if I comment the following line then webpage loads properly in default Android browser.
webview.setWebViewClient(new WebViewClient() {
   public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                 Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
               }
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

    Log.v(tag, "url :" + url);
    view.loadUrl(url);
    return true;
        }
 });
    webview.loadUrl("http://mobile.twitter.com/pawan_rathore88");
Run Code Online (Sandbox Code Playgroud)

谢谢,Pawan

twitter android webview webviewclient

5
推荐指数
1
解决办法
1800
查看次数

Webview - 在显示网站之前更改页面源?

我有一个网站.在网站上,有一个文本"Pic".当我第一次在Webview中显示网站时,我可以将文本更改为"PicGreat".这有效!

但是,稍后当用户点击链接(网站上的某个位置)时,我会将用户转发到新的HTML网站.在我展示网站之前,我想将文本"Pic"更改为"PicGreat".

我该怎么做?我应该编写一个函数,然后在"public boolean shouldOverrideUrlLoading"中调用该函数吗?

我在Stackoverflow上找到了类似的问题,但没有解决. 如何设置webview客户端

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/webview"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
/>
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml中

<uses-permission android:name="android.permission.INTERNET" />
Run Code Online (Sandbox Code Playgroud)

WebTest.java

public class WebTestActivity extends Activity {

 WebView mWebView;
 String rline = "";

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 setContentView(R.layout.main);

 mWebView = (WebView) findViewById(R.id.webview);
 mWebView.getSettings().setJavaScriptEnabled(true);

 HttpURLConnection urlConnection = null;
 try
 {
 URL url = new URL("http://www.picSite.com/");
 urlConnection = (HttpURLConnection) url.openConnection();
 InputStream in = new BufferedInputStream(urlConnection.getInputStream());
 BufferedReader rd = new …
Run Code Online (Sandbox Code Playgroud)

android webview webviewclient

5
推荐指数
1
解决办法
2829
查看次数

Android WebViewClient url 重定向(Android URL 加载系统)

我正在尝试使用以下方法拦截 webview 请求:ShouldInterceptRequest在其中我用来HttpUrlConnection从服务器获取数据,我将其设置为遵循重定向,这对 webviewclient 是透明的。这意味着当我返回 WebResponseResource("", "", data_inputstream) 时,webview 可能不知道目标主机已更改。我怎么能告诉 webview 这发生了?

ourBrowser.setWebViewClient(new WebViewClient() {
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view,
                String url) {
                    ..... //some code omitted here

                HttpURLConnection conn = null;
                try {
                    conn = (HttpURLConnection) newUrl.openConnection();
                    conn.setFollowRedirects(true);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                   ..... //

                InputStream is = null;
                try {
                    is = conn.getInputStream();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return new WebResourceResponse(mimeType, encoding, is);

            }
        }
Run Code Online (Sandbox Code Playgroud)

如果我请求“google.com”,它应该被重定向到“google.co.uk”,但 webview 不知道重定向,如果附加“co.uk”的 css 文件的链接是“/render/something.css” …

redirect android webview android-webview webviewclient

5
推荐指数
1
解决办法
7592
查看次数

WebView的onScaleChanged处理程序多次调用

我有自定义的WebView类,其中默认的zoomIn/zoomOut控件被禁用,我手动处理缩放事件.

我还有WebViewClient,其中onScaleChanged()处理程序被覆盖,它看起来像这样:

mWebViewClient = new WebViewClient(){
    @Override
    public void onScaleChanged(WebView view, float oldScale, float newScale) {
        super.onScaleChanged(view, oldScale, newScale);
        System.out.println("Scale changed");
    }
};
Run Code Online (Sandbox Code Playgroud)

调用zoomIn()/ zoomOut()时会出现问题,而onScaleChanged()处理程序执行多次,我会多次"缩放".每个zoomIn()/ zoomOut()事件应调用一次此事件.

另外值得一提的是,此问题仅在Android版本低于4.4的设备上发生.在Android 4.4上,每个缩放事件只调用一次处理程序(应该是这样).我在Android 4.3.1,Android 4.1.1以及Android 4.0.4(所有设备都是手机)上遇到了这个问题.

众所周知,从Android 4.4引入了Chromium驱动的WebViews,我猜这个更新已经解决了这个问题,但是我无法在Web上找到任何相关内容.

为什么在WebKit引擎支持的WebView上多次调用此处理程序,以及在Chromium支持的WebView上,它按预期工作?

android webview chromium webviewclient android-4.4-kitkat

5
推荐指数
0
解决办法
2008
查看次数

从 WebView 在本机 Soundcloud 应用程序中打开 Soundcloud URL

设想

我的 Android 应用程序中有一个 WebView,其中包含 Soundcloud 嵌入(来自 Embedly)。该嵌入有两个按钮:“在 Soundcloud 上播放”和“在浏览器中收听”。

“在 Soundcloud 上播放”按钮包含以下格式的 URLintent://tracks:257659076#Intent;scheme=soundcloud;package=com.soundcloud.android;end

代码

我的 WebView 使用自定义 WebViewClient (因为我需要拦截一些不同内容的 URL)。

protected class WebViewClient extends android.webkit.WebViewClient {
    public WebViewClient() { }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        PackageManager packageManager = context.getPackageManager();

        // Create an Intent from the URL.
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

        // Find out if I have any activities which will handle the URL.
        List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(intent, 0);

        // If we have an …
Run Code Online (Sandbox Code Playgroud)

android android-webview webviewclient soundcloud

5
推荐指数
1
解决办法
2480
查看次数

如何仅从主页中的WebViewClient的新onReceivedError中检测错误

语境

在Android SDK中23 onReceivedError(WebView view, int errorCode, String description, String failingUrl)已被弃用并替换为onReceivedError(WebView view, WebResourceRequest request, WebResourceError error)。但是根据文档

请注意,与弃用的回调版本不同,新版本将针对任何资源(iframe,图片等)调用,而不仅仅是主页

问题

我们有一个应用程序,在不赞成使用的onReceivedError方法中,有一个代码可以显示本机视图,而不是让用户在WebView中看到错误。

我们想onReceivedError用新方法代替不推荐使用的方法。但是我们不想在本机视图中显示任何资源的错误,而只显示主页。

我们如何在新onReceivedError的页面中识别出错误不是来自主页?

PS 1:我们宁愿没有像解决这个存储主网址,并检查它,失败的URL。

PS 2:如果解决方案是仅使用不推荐使用的方法,那么对于新的Android版本仍可以保证调用该方法?

android android-webview webviewclient

5
推荐指数
2
解决办法
1162
查看次数

onRenderProcessGone(WebView 视图,RenderProcessGoneDetail 详细信息)示例

下面我发布了我当前的onRenderProcessGone

  1. if (!detail.didCrash()) {}实例变量“view”保证为空时,重新初始化它是安全的。我应该自己重新初始化它还是系统会这样做?
  2. 您能否指定应用程序如何继续执行的逻辑示例?如何更优雅地处理崩溃?

        @TargetApi(Build.VERSION_CODES.O)
        @Override
        public boolean onRenderProcessGone(WebView view, RenderProcessGoneDetail detail) {
            // WebViewClient.onRenderProcessGone was added in O.
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
                return false;
            }
            super.onRenderProcessGone(view, detail);
    
            if (!detail.didCrash()) {
                // Renderer was killed because the system ran out of memory.
                // The app can recover gracefully by creating a new WebView instance
                // in the foreground.
                Log.e("MY_APP", "System killed the WebView rendering process " +
                        "to reclaim memory. Recreating...");
    
                if (view != …
    Run Code Online (Sandbox Code Playgroud)

android android-webview webviewclient android-8.0-oreo

5
推荐指数
1
解决办法
2956
查看次数

“选择证书”对话框未显示在 android webview 中

如果我尝试通过可以访问 Android 设备上的证书的 Chrome/Microsoft Edge 浏览器登录 web-url,则一切正常。

chrome/edge 浏览器工作的屏幕截图:

在 Chrome/Microsoft Edge 浏览器中选择证书对话框

我尝试使用 Android 网络视图登录到 android 应用程序上的 web-url,但“选择证书”对话框未显示在 android 网络视图中。

我还遇到过onReceivedClientCertRequest在 API 级别 21 中添加的方法,但我的应用程序需要支持回到 API 级别 17。

  1. onReceivedClientCertRequest方法是否会自动处理“选择证书”对话框?

我对这些证书和“选择证书”对话框有一些常见的疑问。

  1. 如何使用所有 Android API 级别 >= 17 的 web 视图在 android 移动应用程序中实现此类客户端身份验证流程。
  2. 用于此类身份验证的 Android 设备上的可用证书类型是什么。
  3. 如何在 Android 移动应用程序中实现此类身份验证流程,而不使用所有 Android API 级别 >= 17 的 webview。

authentication android ssl-certificate webview webviewclient

5
推荐指数
0
解决办法
194
查看次数