在Android中的shouldoverrideurlloading方法

时间:2013-08-17 13:52:26

标签: android android-webview

我在shouldoverrideurlloading方法中遇到了麻烦,其中未覆盖被覆盖的URL。相反,一个空白的白色屏幕即将出现,Logcat没有显示任何错误。我在下面提供了代码。我犯了什么错误?请指教

package com.example.webviewkm;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.View;
import android.webkit.HttpAuthHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.net.http.SslError;
import android.webkit.SslErrorHandler;

public class MainActivity extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    WebView webView = (WebView) findViewById(R.id.MyWebView);
    webView.setInitialScale(67);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    webView.setVerticalScrollBarEnabled(false);
    webView.setHorizontalScrollBarEnabled(false);

    webView.setWebViewClient(new WebViewClient() {

        @Override 
        public void onReceivedLoginRequest(WebView view, String realm, 
                        String account, String args) { 


                System.err.println(realm); 
                System.err.println(account); 
                System.out.println(args); }

        @Override
        public void onReceivedError( WebView view, int errorCode, String description, String failingUrl ) {
            Toast.makeText(view.getContext(), "Unknown Error", Toast.LENGTH_LONG).show();
            System.err.println(errorCode + " - " + description + "-" + failingUrl); 


        }
         @Override
            public void onReceivedSslError( WebView view, SslErrorHandler handler, SslError error ) {
              System.err.println("SSL ERROR");  

             handler.proceed();
            }
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Intent intent = new Intent(MainActivity.this, MainActivity.class);
        intent.putExtra("url", url);
        startActivity(intent);
        return true;
      }

      @Override
      public void onReceivedHttpAuthRequest(WebView view, final HttpAuthHandler handler, final String host,
          String realm) {
          System.err.println("HTTP auth request"); 
        final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);        


        if (handler.useHttpAuthUsernamePassword()) {
          if (preferences.contains("username")) {
            handler.proceed(preferences.getString("username", null), preferences.getString("password", null));
            return;
          }

        }

        new Dialog(MainActivity.this){
          @Override
          protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setTitle(host);
            setContentView(R.layout.credentials);


            final EditText userName = (EditText) findViewById(R.id.UserName);
            final EditText password = (EditText) findViewById(R.id.Password);
            userName.setText(preferences.getString("username", ""));
            password.setText(preferences.getString("password", ""));

            Button button = (Button) findViewById(R.id.GoButton);
            button.setOnClickListener(new Button.OnClickListener(){

              public void onClick(View v) {
                String userName2 = userName.getText().toString();
                String password2 = password.getText().toString();
                Editor edit = preferences.edit();
                edit.putString("username", userName2);
                edit.putString("password", password2);
                edit.commit();
                handler.proceed(userName2, password2);
                dismiss();
              }});
          }

        }.show();
      }
    });

    String url;
    if (getIntent().hasExtra("url")) {
      url = getIntent().getStringExtra("url");
    } else {
     url = "https://kee.mahindrasatyam.com/_layouts/mobile/mobilesearch.aspx";


    }
    webView.loadUrl(url);

  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    Intent intent = new Intent(MainActivity.this, MainActivity.class);
    startActivity(intent);
    return true;
  }

}

1 个答案:

答案 0 :(得分:5)

在下面的代码中粘贴了方法shouldOverrideUrlLoading,

public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Intent intent = new Intent(MainActivity.this, MainActivity.class);
        intent.putExtra("url", url);
        startActivity(intent);
        return true;
      }

你刚刚开始使用相同的活动,并且你将url作为额外的意图传递,这不会在webview中加载url,也不会给你一个错误,如果你打算在webview中加载url,使用< / p> 在shouldOverrideUrlLoading方法中

webView.loadUrl(url)