Java – All in One → Java URL – Camposha

Java – All in One

Java – All in One

Java – All in One

Java URL

Java URL Tutorial and Examples.

1. Get Host From a String url

We’ll wrap it in a try catch block so as to catch MalformedURLException.

    public static String getHost(String urlstr){

        try {
            URL u = new URL(urlstr);
            String ret=u.getProtocol()+"://"+u.getHost();
            return ret;
        } catch (MalformedURLException e) {

            return "";
        }

    }
2. Get Host Without Protocol
    public static String getHostOnly(String urlstr){

        try {
            URL u = new URL(urlstr);
            String ret=u.getHost();
            return ret;
        } catch (MalformedURLException e) {

            return "";
        }

    }
3. Add Query Map
    public static String addQueryMap(HashMap<String, String> queryMap, String url) {
        Iterator iter = queryMap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();

            try {
                String q = (String) entry.getValue();
                String e = URLEncoder.encode(q, "utf-8");
                url += "&" + entry.getKey() + "=" + e;

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return url;
    }
4. Get Query String From HashMap
    public static String getQueryString(HashMap<String, String> queryMap) {
        String url = "";
        Iterator iter = queryMap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();

            try {
                String q = (String) entry.getValue();
                String e = URLEncoder.encode(q, "utf-8");
                url += "&" + entry.getKey() + "=" + e;

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return url;

    }
5. Get query String From a List
    public static String getQueryString(List<NameValuePair> queryMap) {
        String url = "";
        for(NameValuePair p:queryMap){
            try {
                String q = p.getValue();
                if(q!=null&&!q.equals("")) {
                    String e = URLEncoder.encode(q, "utf-8");
                    url += "&" + p.getName() + "=" + e;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return url;

    }
6. Encode strings
    public static String encode(String w, String s) {
        try {
            return URLEncoder.encode(w,s);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }
    }
7. How to Validate a URL
    public static boolean isValideUrl(String url) {
        if(url==null){
            return false;
        }
        if(url.startsWith("http://")||url.startsWith("https://")){
            return true;
        }
        return false;
    }
8. Open Web Browser in any OS

Leave a Reply

Your email address will not be published. Required fields are marked *

Rating

Not enough ratings to display
Join Us
X