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

/**
 * URL Encodes the given String <code>s</code> using the UTF-8 character
 * encoding./*from  w w w. j a  v  a  2s  .  c om*/
 * 
 * @param s a String
 * @return url encoded string
 */
public static String encode(final String s) {
    if (s == null) {
        return null;
    }
    try {
        return URLEncoder.encode(s, "UTF-8");
    } catch (final UnsupportedEncodingException ignored) {
    }
    return null;
}

From source file:Main.java

public static String prepareParameterizedGetUrl(String url, List<NameValuePair> params) {

    url += "?";//from   w w  w . jav  a 2s  .c o  m
    if (params != null) {
        for (NameValuePair pair : params) {
            try {
                url += pair.getName() + "=" + URLEncoder.encode(pair.getValue(), "UTF-8") + "&";
            } catch (UnsupportedEncodingException e) {

                e.printStackTrace();
                break;
            }

        }
        url = url.substring(0, url.trim().length() - 1);
    }
    return url;
}

From source file:Main.java

public static String encodeUrl(String url) {
    String new_url = url;
    Matcher matcher = Pattern.compile("[\\u4e00-\\u9fa5]").matcher(new_url);
    while (matcher.find()) {
        try {/*ww w . j a v a2  s .c om*/
            new_url = new_url.replaceAll(matcher.group(), URLEncoder.encode(matcher.group(), "gb2312"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    return new_url;
}

From source file:Main.java

private static String setParams(String msg) {
    String url = "";
    try {// w w  w . j av a 2s .  c om
        url = URL + "?key=" + API_KEY + "&info=" + URLEncoder.encode(msg, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return url;
}

From source file:Main.java

public static String encodeParamsToUrl(String originUrl, Map<String, String> params) {
    String paramsEncoding = "UTF-8";
    StringBuilder encodedParams = new StringBuilder();
    try {//from  w ww. j  a v a  2  s.c om
        for (Map.Entry<String, String> entry : params.entrySet()) {
            encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
            encodedParams.append('=');
            encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
            encodedParams.append('&');
        }
        return originUrl + "?" + encodedParams.toString();
    } catch (UnsupportedEncodingException uee) {
        throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
    }
}

From source file:Main.java

static String map2UrlQueryString(Map<String, Object> map) {
    StringBuilder sb = new StringBuilder();
    for (HashMap.Entry<String, Object> e : map.entrySet()) {
        try {//w w  w . j  av  a  2 s .c o m
            sb.append(e.getKey());
            sb.append('=');
            sb.append(URLEncoder.encode(String.valueOf(e.getValue()), "UTF-8"));
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        sb.append('&');
    }
    if (sb.length() == 0)
        return "";
    else
        return sb.substring(0, sb.length() - 1);
}

From source file:Main.java

public static String spec(String host, String botid, String custid, String input) {
    //System.out.println("--> custid = "+custid);
    String spec = "";
    try {//ww w.  j  ava2s .c  o  m
        if (custid.equals("0")) // get custid on first transaction with Pandorabots
            spec = String.format("%s?botid=%s&input=%s", "http://" + host + "/pandora/talk-xml", botid,
                    URLEncoder.encode(input, "UTF-8"));
        else
            spec = // re-use custid on each subsequent interaction
                    String.format("%s?botid=%s&custid=%s&input=%s", "http://" + host + "/pandora/talk-xml",
                            botid, custid, URLEncoder.encode(input, "UTF-8"));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    System.out.println(spec);
    return spec;
}

From source file:Main.java

public static String encode(String value, String charset) {
    String result = null;//  w  w w.  j  a v  a2  s. co m
    if (!TextUtils.isEmpty(value)) {
        try {
            result = URLEncoder.encode(value, charset);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return result;
}

From source file:Main.java

/**
 * unreserved characters (ALPHA / DIGIT / "-" / "." / "_" / "~") MUST not be encoded, 
 * others MUST be encoded./*w  w w  . ja v  a  2  s.  com*/
 * @param str
 * @return percent encoding defined in RFC 5849 3.6. Percent Encoding
 */
private static String oauthEncode(String str) {
    try {
        return URLEncoder.encode(str, ENCODE).replace("*", "%2A").replace("%7E", "~").replace("+", "%20");
    } catch (UnsupportedEncodingException e) {
        return null;
    }
}

From source file:Main.java

private static String urlEncode(String str, String charset) throws UnsupportedEncodingException {
    Pattern p = Pattern.compile("[\u4e00-\u9fa5]+");
    Matcher m = p.matcher(str);/*  ww  w  . j a  v a 2s.co  m*/
    StringBuffer b = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(b, URLEncoder.encode(m.group(0), charset));
    }
    m.appendTail(b);
    return b.toString();
}