12

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

Ali
  • 835
  • 1
  • 10
  • 23
  • "app even not opens mainActivity" do you get some kind of error or crash or just a blank screen? – hypd09 Jan 17 '14 at 11:58
  • Can you provide a logcat? – PsyGik Jan 17 '14 at 11:59
  • First I need to ask what is "realm", its being use in setHttpAuthUsernamePassword function.If I am missing some value there before sharing logcat? – Ali Jan 17 '14 at 12:02
  • Right now nothing is coming in logcat nor the app is opening.Howeverm getting this in console :Success! Starting activity com.commonsware.android.appwidget.lorem.LoremActivity on device 4df1e6190ef38f11 ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.commonsware.android.appwidget.lorem/.LoremActivity } – Ali Jan 17 '14 at 12:06
  • I would say it is a duplicate question. Check the answer for your question here (most voted one): http://stackoverflow.com/questions/2585055/using-webview-sethttpauthusernamepassword – Alex P Jan 17 '14 at 12:19

4 Answers4

26
webview.setWebViewClient(new MyWebViewClient ());

private class MyWebViewClient extends WebViewClient {
    @Override
    public void onReceivedHttpAuthRequest(WebView view,
            HttpAuthHandler handler, String host, String realm) {
        handler.proceed("me@test.com", "mypassword");
    }
}

This is most voted solution there I am not sure where to set the URL to open.Please suggest.

David Manpearl
  • 12,362
  • 8
  • 55
  • 72
Ali
  • 835
  • 1
  • 10
  • 23
  • Have you tried adding `webView.loadUrl("http://3864.cloud-matic.net/");` after setting web view client? – Alex P Jan 17 '14 at 20:39
  • Hi, Yes I am using this in my onCreate method. webView.loadUrl("http://3864.cloud-matic.net/"); webView.setWebViewClient(new MyWebViewClient()); – Ali Jan 20 '14 at 06:07
  • You need to provide URL with `http://` when calling `loadUrl`, i.e. `webView.loadUrl("http://3864.cloud-matic.net");` – Alex P Jan 20 '14 at 19:40
  • Alex I am doing the same as you mentioned and providing correct credentials , still its say 401 authorization required.The same password and username are working when I try to login from web browser. – Ali Jan 21 '14 at 05:20
  • Ali, if it works in the browser and with this method you still get 401 via your app, it may be the case that you are not using `Basic` authentication, but `Digest`. Could you confirm in the browser console what you are sending from the client, is it `Authorization: Basic` or `Authorization: Digest`? – Alex P Jan 21 '14 at 21:47
2

Based on answer in another topic I made it in a little different way.

This solution is working also in case, that you have auto login form displayed, when authentication failed (in such case, solution with overriding 'onReceivedHttpAuthRequest' is not working, because you are getting a normal html page without error).

String up = "yourusername"+":"+"yoursuperstrongpassword";
String authEncoded = new String(Base64.encodeBase64(up.getBytes()));
String authHeader = "Basic "+authEncoded;
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", authHeader);
myWebView.loadUrl("https://192.168.137.1:8443/tmedserver/doctor/doctorConsultations/consultationCall.mvc?consultationId=23", headers);
Krystian
  • 2,221
  • 1
  • 26
  • 41
2

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.

pereckeSokSzam
  • 111
  • 1
  • 5
1

This should be perfect code snippet.

    webView.getSettings().setJavaScriptEnabled(true);
    webView.setFocusable(true);
    webView.loadUrl("http://3864.cloud-matic.net/");
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onReceivedHttpAuthRequest(WebView view,
                                              HttpAuthHandler handler, String host, String realm) {
            handler.proceed("me@test.com", "mypassword");
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            prDialog.setMessage("Please wait ...");
            prDialog.show();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (prDialog != null) {
                prDialog.dismiss();
            }
        }
    });