Example usage for java.net URLEncoder encode

List of usage examples for java.net URLEncoder encode

Introduction

In this page you can find the example usage for java.net URLEncoder encode.

Prototype

public static String encode(String s, Charset charset) 

Source Link

Document

Translates a string into application/x-www-form-urlencoded format using a specific java.nio.charset.Charset Charset .

Usage

From source file:Main.java

public static String getSmbServerUrl(String[] auth, boolean anonym) {
    String smbFilePath;/*from   ww  w . java  2  s.co  m*/
    try {
        String yourPeerIP = auth[0], domain = "";
        smbFilePath = "smb://" + (anonym ? "" : (URLEncoder.encode(auth[1] + ":" + auth[2], "UTF-8") + "@"))
                + yourPeerIP + "/";
        return smbFilePath;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String utf8Encode(String str) {

    if (!isEmpty(str) && str.getBytes().length != str.length()) {
        try {/*from   w  w  w.  j  a  v a  2s .c  o  m*/
            return URLEncoder.encode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("UnsupportedEncodingException occurred. ", e);
        }
    }
    return str;
}

From source file:Main.java

/**
 * Encode some text to be used in a URL.
 * @param s the string to encode.//from  w ww.j  av a  2 s.  c  om
 * @return the string once encoded.
 */
@SuppressWarnings("deprecation")
public static String encodeURL(String s) {
    String encodedString;
    try {
        encodedString = URLEncoder.encode(s, "UTF-8");
    } catch (Exception e) {
        encodedString = URLEncoder.encode(s);
    }
    return encodedString.replaceAll("\\+", "%20");
}

From source file:Main.java

public static String toURLEncoded(String paramString) {
    if (paramString == null || paramString.equals("")) {
        return paramString;
    }/*ww w.  j a  va2  s  . c  o  m*/
    try {
        String str = new String(paramString.getBytes(), "UTF-8");
        str = URLEncoder.encode(str, "UTF-8");
        return str;
    } catch (Exception localException) {
    }

    return paramString;
}

From source file:Main.java

/**
 * Returns encoded String/*from w w w  .j  a  v a2 s .  c o  m*/
 *
 * @param sUrl
 *            , input string
 * @return , encoded string
 */
public static String urlEncode(String sUrl) {
    try {
        return URLEncoder.encode(sUrl, "UTF-8").replace("+", "%20");
    } catch (UnsupportedEncodingException e) {
        return null;
    }
}

From source file:Main.java

public static String getQueryUrl(String word) throws UnsupportedEncodingException {
    return URL_HOST + "/pl?key=" + URLEncoder.encode(word, "UTF-8");
}

From source file:Main.java

/**
 * Encode a String of URL into UTF-8, and handles exception by returning a default value.
 * @param url The URL to encode/*from   www.ja  va2 s. c  o m*/
 */
public static String urlEncode(String url, String def) {
    try {
        return URLEncoder.encode(url, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        Log.w("Transit URL encode", "Unsupported encoding: " + e.getMessage());
        return def;
    }
}

From source file:Main.java

public static String toURLEncoded(String paramString) {
    if (paramString == null || paramString.equals("")) {
        Log.d("h", "toURLEncoded error:" + paramString);
        return "";
    }/*from  w w w  .  j  a  va 2  s  .c o m*/

    try {
        String str = new String(paramString.getBytes(), "UTF-8");
        str = URLEncoder.encode(str, "UTF-8");
        return str;
    } catch (Exception localException) {
        Log.d("h", "toURLEncoded error:" + paramString, localException);
    }

    return "";
}

From source file:Main.java

public static String createUri(final String path) {
    if (path.contains(":/")) {
        return path;
    }//  w  w  w .  j  a v  a 2  s . c  om

    try {
        return DUMMY_PREFIX + URLEncoder.encode(path, "UTF-8");
    } catch (final UnsupportedEncodingException e) {
        throw new AssertionError(e);
    }
}

From source file:Main.java

public static String realUrl(String target) {
    try {/*from  w w  w  . ja  v a2  s  .  c  o  m*/
        URL url = new URL(target);

        String protocol = url.getProtocol();
        String host = url.getHost();
        String path = url.getPath();
        String query = url.getQuery();

        path = URLEncoder.encode(path, "utf-8").replace("%3A", ":").replace("%2B", "+").replace("%2C", ",")
                .replace("%5E", "^").replace("%2F", "/").replace("%21", "!").replace("%24", "$")
                .replace("%25", "%").replace("%26", "&").replace("%28", "(").replace("%29", ")")
                .replace("%40", "@").replace("%60", "`");
        // .replace("", "#"); // not support.

        StringBuilder urlBuild = new StringBuilder(protocol).append("://").append(host).append(path);
        if (query != null)
            urlBuild.append("?").append(query);
        return urlBuild.toString();
    } catch (IOException e) {
        return target;
    }
}