Android-WebView路由登录192.168.1.1
2016-02-01 13:59
651 查看
因为要实现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 中的
发现以上方法没有一个执行到了
然后我在对比火狐,IE,Google Chrome感觉是浏览器自己做的
浏览器打开的时候会提示输入用户名和密码
发现WebViewClient中有一个方法 onReceivedHttpAuthRequest
然后查看源码
发现是直接调用的 handler.cancel();
给WebView增加WebViewClient 重写 onReceivedHttpAuthRequest 将此方法置空
发现登录的时候会停止在一个空白页面,等待处理,并没有像开始直接打开的时候提示登录失败
然后查看onReceivedHttpAuthRequest中的参数有一个HttpAuthHandler
源码默认是调用的cancel() 发现还有一个方法 proceed(String username, String password)
然后在重写 onReceivedHttpAuthRequest时
然后就登录进去了我的路由器,问题到这里已经解决
onReceivedHttpAuthRequest这个方法也是在主线程中的,可以在这里制做自己的登录窗口
重写WebViewClient
创建MyWebChromeClient 重写 WebChromeClient
创建MyWebViewClient 重写 WebViewClient
摘自:http://www.cnblogs.com/smile365/p/4130983.html 请尊重版权,非常感谢作者
自己查找资料,根据大神的方法不断丰富,也当时自己的经验总结。
主要遇到以下问题:
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 请尊重版权,非常感谢作者
相关文章推荐
- Android框架模式之MVC与MVP
- MTK平台Android4.4 拍照默认图片格式修改
- Android-Sql-数据性能优化
- Android studio Debug效率提升
- android *** Layout 06 RadioButton && ToggleButton && Switch
- ios学习 Mac下获取AppStore安装包文件路径
- 关于微信 init接口返回的Count和webwxbatchgetcontact post请求的参数Count
- Swift 嵌套类型
- Android开发之BUG专讲:入门篇(二)
- 自定义控件之------仿ios下拉回弹效果
- cocos2dx 3.3 + QT5.3制作游戏编辑器
- iOS 定义具有位移操作的枚举的意义
- iOS手势操作简介(一)
- 关于AndroidManifest.xml中的android:debuggable
- Android AES加密/解密代码
- iOS 基于WebService开发
- Android常用开源工具(1)-Dagger2入门
- webview常用方法
- Android中的SharedPreferences陷阱 [转]
- 10.Swift 中的闭包