Validate the URL

Have a great day friends. Today will look up validating the URL. How to do this?

Most of times, your internet will be working, but sometimes URL to be busy or invalid. In this case, we follow this thing.

 public boolean isServerReachable(Context context, String url) {
        ConnectivityManager connMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = connMan.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnected()) {
            try {
                URL urlServer = new URL(url);
                if(url.startsWith("https")){
                    HttpsURLConnection urlConn = (HttpsURLConnection) urlServer.openConnection();
                    TLSSocketFactory socketFactory = new TLSSocketFactory();
                    urlConn.setSSLSocketFactory(socketFactory);
                    urlConn.setConnectTimeout(3000); //<- 3Seconds Timeout
                    urlConn.connect();
                    if (urlConn.getResponseCode() == 200) {
                        return true;
                    } else {
                        return false;
                    }
                }else if(url.startsWith("http")){
                    HttpURLConnection urlConn = (HttpURLConnection) urlServer.openConnection();
                    urlConn.setConnectTimeout(3000);
                    urlConn.connect();
                    if(urlConn.getResponseCode()==200){
                        return true;
                    }else{
                        return false;
                    }
                }
            } catch (MalformedURLException e1) {
                return false;
            } catch (IOException e) {
                return false;
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (KeyManagementException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

I'm using the async task to validate. that's your own choice.

 private void checkUrl(final String url) {
        new AsyncTask<String, Void, Boolean>() {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                Log.i("url_", url);
                progressDialog.setMessage("Validating URL...");
                progressDialog.show();
            }

            @Override
            protected Boolean doInBackground(String... params) {
                return isServerReachable(getContext(), params[0]);
            }

            @Override
            protected void onPostExecute(Boolean result) {
                super.onPostExecute(result);
                progressDialog.dismiss();
                if (result) {
                    dismiss();
                } else {
                    Utils.showToast(getContext(), "URL not valid");
                }
            }
        }.execute(url);
    }

First we need to check the entered URL is valid or invalid,

    private boolean isValidUrl(String url) {
        Pattern p = Patterns.WEB_URL;
        Matcher m = p.matcher(url.toLowerCase());
        return m.matches();
    }

Finally will implement like this.

// first check the entered URL is valid,
if (isValidUrl(url)) {
// then check the url is valid to pass.
checkUrl(url);
} else {
Utils.showToast(getContext(), "Enter the valid URL");
}

Comments

Popular posts from this blog

Flutter Bloc - Clean Architecture

Dependencies vs Dev Dependencies in Flutter

What's new in android 14?