hello world
hello world

Reputation: 807

Check for Url Http status Android webview

Is there a way if I can check status code for the url I load into a webview? I want to check if the url has anymore redirects, and if not I want to execute a piece of code.

Iam implementing the WebViewClient and I want to execute a code in the onPageFinished();

EDIT: The url I load is a login screen. I want to check if the user has successfully logged in.

How can I achieve this?

Upvotes: 4

Views: 2732

Answers (3)

mr.chen
mr.chen

Reputation: 9

Try this

public void evaluateJavascript (String script, ValueCallback<String> resultCallback)

Upvotes: -1

Nas
Nas

Reputation: 2198

webView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if(url.isEmpty()){
                    // do your stuff as you want.
                }
                return true;
            }

            public void onPageFinished(WebView view, String url) {
                // do your stuff as you want
            }

            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {

            }
        });

Upvotes: 2

Nanoc
Nanoc

Reputation: 2381

Use HttpURLConnection class.

URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();

int code = connection.getResponseCode();

There you have the http code returned.

Hope this helps.

Upvotes: 1

Related Questions