我的开发环境落后于HTTP代理.在Android模拟器中,我可以在wifi首选项中设置我的代理地址和凭据.然后,当我在浏览器中查看该页面时,系统会提示我重新输入我指定的主机的凭据.不知道为什么我必须再次输入它们,但它可以工作,我可以查看该页面.
然后我有一个带有WebView的应用程序加载相同的页面.该应用程序具有Internet权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Run Code Online (Sandbox Code Playgroud)
并支持平台通知:
WebView.enablePlatformNotifications();
Run Code Online (Sandbox Code Playgroud)
但是......当我尝试查看页面时,我收到代理的错误页面,说明没有正确输入用户名和密码.这告诉我应用程序正在成功读取代理地址,但是浏览器没有读取凭据.然后,在完成请求之前,它无法显示与浏览器请求凭据相同的对话框.
有没有办法启用此对话框(内置?)或手动指定代理详细信息的方法?
查看android浏览器源代码,以下内容很清楚:
onReceivedHttpAuthRequest的连接WebViewClient与host设置为"{proxy}:{port}"和realm设置为空字符串.因此,从Android浏览器复制代码和对话框布局的最简单方法是:
WebViewClient和复制onReceivedHttpAuthRequest方法的新类com.android.browser.Controller.修改方法不依赖mPagesDialogHandler. context是你的活动.
HttpAuthenticationDialog dialog = new HttpAuthenticationDialog(context, host, realm);
dialog.setOkListener(new HttpAuthenticationDialog.OkListener() {
public void onOk(String host, String realm, String username, String password) {
handler.proceed(username, password);
}
});
dialog.setCancelListener(new HttpAuthenticationDialog.CancelListener() {
public void onCancel() {
handler.cancel();
}
});
dialog.show();
Run Code Online (Sandbox Code Playgroud)将此新内容WebViewClient用于您的Web视图.
| 归档时间: |
|
| 查看次数: |
3918 次 |
| 最近记录: |