Android-WebView路由登录192.168.1.1

因为要实现webview访问登录框,类似路由器设置界面这一类的东西。遇到了许多问题。而查资料后,发现很多没有解决,而解决了也不是很详细。
自己查找资料,根据大神的方法不断丰富,也当时自己的经验总结。

主要遇到以下问题:

1. Webview无法显示弹出框。解决通过重写该方法onReceivedHttpAuthRequest。

2.无法显示JAVASCRIPT的内容。webView.setFocusable(true);



由于项目需求制做路由器的登录,有的路由可以直接打开网页登录,有的是打开的登录窗

我们的路由是打开的登录窗口,我在自己写的时候用WebView打不开登录窗

在网上搜索的答案是
WebView.setHttpAuthUsernamePassword(host, realm, username, password)
WebView增加WebChromeClient重写onJsAlert
发现以上办法都不能解决问题
 
用浏览器打开的时候会弹出登录框,当我用Android WebView登录的时候提示登录失败
打不开登录框,一开始我以为是js生成的弹出框
试验给WebView setWebChromeClient 
重写 WebChromeClient 中的
public boolean onJsAlert(WebView view, String url, String message, JsResult result) 
public boolean onJsConfirm(WebView view, String url, String message, JsResult result)
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result)
public boolean onJsBeforeUnload(WebView view, String url, String message, JsResult result)
public boolean onJsTimeout()
发现以上方法没有一个执行到了
然后我在对比火狐,IE,Google Chrome感觉是浏览器自己做的
浏览器打开的时候会提示输入用户名和密码
 
发现WebViewClient中有一个方法 onReceivedHttpAuthRequest
然后查看源码
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) { 
        handler.cancel();
}
发现是直接调用的 handler.cancel();
给WebView增加WebViewClient 重写 onReceivedHttpAuthRequest 将此方法置空
发现登录的时候会停止在一个空白页面,等待处理,并没有像开始直接打开的时候提示登录失败
 
然后查看onReceivedHttpAuthRequest中的参数有一个HttpAuthHandler 
源码默认是调用的cancel() 发现还有一个方法 proceed(String username, String password)
 
然后在重写 onReceivedHttpAuthRequest时
public void onReceivedHttpAuthRequest(WebView view,  HttpAuthHandler handler, String host, String realm) { 
        handler.proceed("admin", "admin");
} // PS: admin 是我路由器默认的帐号和密码

然后就登录进去了我的路由器,问题到这里已经解决

onReceivedHttpAuthRequest这个方法也是在主线程中的,可以在这里制做自己的登录窗口


重写WebViewClient 


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    init();
}

private void init(){

    /*设置WebViewClient*/
    webView = (WebView) findViewById(R.id.webView);
    webView.setWebViewClient(new MyWebViewClient());

    /*设置WebChromeClient*/
    MyWebChromeClient myWebChromeClient = new MyWebChromeClient();
    webView.setWebChromeClient(myWebChromeClient);

    //WebView加载web资源
    webView.loadUrl("http://10.16.52.32/");

    /*设置支持JAVASCPRIT*/
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    /*设置获取焦点*/
    webView.setFocusable(true);
}

创建 MyWebChromeClient 重写  WebChromeClient 
/**
 * Created by hp on 2016-2-1.
 */
public class MyWebChromeClient extends WebChromeClient {
    @Override
    public boolean onJsAlert(WebView view, String url, String message, JsResult jsResult) {
        final JsResult finalJsResult = jsResult;
        new AlertDialog.Builder(view.getContext()).setMessage(message).setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finalJsResult.confirm();
            }
        }).setCancelable(false).create().show();
        return true;
    }
}

创建 MyWebViewClient  重写  WebViewClient 


public class MyWebViewClient extends WebViewClient {

    //override
    public void onReceivedHttpAuthRequest(WebView view,  HttpAuthHandler handler, String host, String realm) {
        handler.proceed("admin", "sunlight");
        int d = Log.d("MyWebViewClient", "onReceivedHttpAuthRequest");
    }

    //override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

// 使用自己的WebView组件来响应Url加载事件,而不是使用默认浏览器器加载页面

        view.loadUrl(url);
        Log.d("MyWebViewClient", "shouldOverrideUrlLoading");
// 记得消耗掉这个事件。给不知道的朋友再解释一下,Android中返回True的意思就是到此为止吧,事件就会不会冒泡传递了,我们称之为消耗掉

        return true;

    }
}



摘自:http://www.cnblogs.com/smile365/p/4130983.html 请尊重版权,非常感谢作者

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值