3

在我的应用程序中,我有一个包含 Web 视图的活动。当我请求打开连接时,服务器会重定向我,但我的 Web 视图从未得到它并显示找不到页面消息。当我在 PC 的浏览器中测试 URL 时,它会将我定向到下一页。但是,在 webView 中,我从未定向到第二页。我的流程应该是这样的: 1- 打开一个 URL 2- 服务器指向下一个 URL 3- 通过应用程序在该页面中填写登录表单 4- 获取服务器响应

我的代码是这样的:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_loginweb);

    Log.i(TAG, "Try to create activity...");

    final String userId = PropertyManager.getUserId();
    final String password = PropertyManager.getPassword();
    Log.i(TAG, "Id: " + userId + ", Password: " + password);

    final WebView webView = (WebView) findViewById(R.id.webView);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.addJavascriptInterface(new JavaScriptInterface(this), "JsInterface");
    webView.setWebViewClient(new WebViewClient() {  
        @Override  
        public void onPageFinished(WebView view, String url) {  
            webView.loadUrl("javascript:document.LoginForm.tempusername.value = '" + userId + "';");
            webView.loadUrl("javascript:document.LoginForm.password.value = '" + password + "';"); 
            webView.loadUrl("javascript:document.LoginForm.Submit.click();");

            webView.loadUrl("javascript:window.JsInterface.processHTML(document.getElementsByTagName('html')[0].innerHTML);");

            String currentUrl = webView.getUrl();
            Log.i(TAG, "current Url is: " + currentUrl);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            super.shouldOverrideUrlLoading(view, url);

            Log.i(TAG, "redirect to: " + url);
            view.loadUrl(url);
            return false;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);

            Log.i(TAG, "error code is:" + errorCode);
        }
    });  

    webView.loadUrl(PropertyManager.getLoginPageURL());
}


/*
 * To bind a new interface between our JavaScript and Android code, call addJavascriptInterface(), 
 * passing it a class instance to bind to JavaScript of login page and an interface name that 
 * JavaScript can call to access the class.
 */
public class JavaScriptInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    JavaScriptInterface(Context c) {
        mContext = c;
    }

    /** process the html as needed by the app */
    public void processHTML(String html) {
        Log.i(TAG, "Processing HTML...");
        Log.i(TAG, "content of page: " + html);
    }

    /** Show a toast from the web page */
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}

我的问题是shouldOverrideUrlLoading()方法在重定向期间从不调用。任何建议将不胜感激。谢谢。

4

1 回答 1

4

问题是因为证书过期。通过添加以下方法很容易绕过webView.setWebViewClient(new WebViewClient() { });

@Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
//              super.onReceivedSslError(view, handler, error);

                Log.e(TAG, "onReceivedSslError...");
                Log.e(TAG, "Error: " + error);
                handler.proceed();
            }

此外,为了使用此方法,您的 API 级别应为 8 或更高。

于 2012-08-23T07:54:23.933 回答