3

私のアプリケーションには、Web ビューを含むアクティビティがあります。接続サーバーを開くように要求すると、リダイレクトされますが、Web ビューはそれを取得せず、ページが見つからないというメッセージを表示します。PC のブラウザで URL をテストすると、次のページに移動します。ただし、webView では、2 ページ目に移動することはありませんでした。私のフローは次のようになります: 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 に答える