checks and ensure http/https protocol in URL - Android java.net

Android examples for java.net:URL

Description

checks and ensure http/https protocol in URL

Demo Code

import android.util.Log;
import java.net.URLDecoder;
import java.util.Collection;
import java.util.regex.Pattern;

public class Main{

    /**//from w  w  w .  j  a  v  a2 s .c om
     * This method checks and ensure http/https protocol in URL
     * 
     * @param url
     * @return formattedUrl
     */
    public static String getFormattedURL(String url) {
        if (url.indexOf("http://") == 0 || url.indexOf("https://") == 0) {
            return url;
        } else if (url.indexOf("://") == 0) {
            return "http" + url;
        } else if (url.indexOf("//") == 0) {
            return "http:" + url;
        } else {
            return "http://" + url;
        }
    }

}

Related Tutorials