Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BasicAuthentication in android for webview not working

Hi I want to open this url http://3864.cloud-matic.net/ from my android webview and I have tried many ways but the app even not opens mainActivity. What I have tried is below.

 public void onCreate(Bundle state) {
    super.onCreate(state);
    setContentView(R.layout.main);
    WebView webView = (WebView) findViewById(R.id.webView1);
    webView.setHttpAuthUsernamePassword("cloud-matic.net/",     "realm", username, password);
    webView.loadUrl("http://3864.cloud-matic.net/");
}

Please give me idea where I am wrong. Ali

like image 529
Ali Avatar asked Jan 17 '14 11:01

Ali


1 Answers

Not sure if this will help someone, but I implemented the onReceivedHttpAuthRequest function(like the people above) with username and password and in every device it worked normally except on Android Oreo. Then, I realized that the Oreo phone received http requests not from the main frame and it gave me 401 error.

In the onReceivedHttpError function I checked, if the http error is from the main frame with:

         `override fun onReceivedHttpError(
                view: WebView?,
                request: WebResourceRequest?,
                errorResponse: WebResourceResponse?
            ) {
                super.onReceivedHttpError(view, request, errorResponse)
                if(request.isForMainFrame){ //do smt. with the error}
               }`

Then it started to work normally.

like image 135
pereckeSokSzam Avatar answered Sep 21 '22 14:09

pereckeSokSzam