user2685516
user2685516

Reputation: 51

Shouldoverrideurlloading method in android

I am running into trouble in shouldoverrideurlloading method, wherein the URL being overridden is not being shown. Instead, a blank white screen is coming up, and Logcat shows no errors. I have provided code below . WHere am I making a mistake? Please advise

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;
  }

}

Upvotes: 0

Views: 16251

Answers (1)

Nargis
Nargis

Reputation: 4787

In your Code pasted below in the method shouldOverrideUrlLoading,

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

You are just Starting the same Activity and you are passing the url as intent extra ,this will not load the url in webview neither it will give you an error , and if you are intending to load the url in the webview use

webView.loadUrl(url) in shouldOverrideUrlLoading method.

Upvotes: 5

Related Questions