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 getQueryUrl(String word, Integer page) throws UnsupportedEncodingException {
    if (word == null) {
        return null;
    }/*from w w  w  .  j a  va  2 s. com*/
    String qp = (page == null) ? "" : ("&page=" + page);
    return URL_HOST + "/search/" + URLEncoder.encode(word, "UTF-8") + "?sort=v&order=d" + qp;
}

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 {//from w ww  .  j  a  v  a2  s. co  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 final String mapToQueryString(HashMap<String, String> queryString) {
    StringBuilder sb = new StringBuilder();
    try {/*  w w  w .ja v a 2 s.c o  m*/
        for (HashMap.Entry<String, String> e : queryString.entrySet()) {
            if (sb.length() > 0) {
                sb.append('&');
            }
            sb.append(URLEncoder.encode(e.getKey(), "UTF-8")).append('=')
                    .append(URLEncoder.encode(e.getValue(), "UTF-8"));
        }
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
    }
    return sb.toString();
}

From source file:Main.java

public static String encodePath(String path, String delimiter, String characterSet)
        throws UnsupportedEncodingException {
    final StringTokenizer tokenizer = new StringTokenizer(path, delimiter);
    final StringBuilder builder = new StringBuilder(path.length());

    while (tokenizer.hasMoreTokens()) {
        builder.append(URLEncoder.encode(tokenizer.nextToken(), characterSet));
        builder.append(delimiter);//from  w ww.j  a v a 2s  .c o  m
    }
    builder.delete(builder.length() - delimiter.length(), builder.length());
    return builder.toString();
}

From source file:Main.java

private static String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for (Map.Entry<String, String> entry : params.entrySet()) {
        if (first)
            first = false;/*  ww  w  . j  a  v a 2s . c  o m*/
        else
            result.append("&");

        result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
    }

    return result.toString();
}

From source file:Main.java

public static String toURLEncoded(String paramString) {
    if (paramString == null || paramString.equals("")) {
        LogD("toURLEncoded error:" + paramString);
        return "";
    }/*from ww  w  .  ja  v  a  2s .  com*/

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

    return "";
}

From source file:Main.java

public static String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;

    for (NameValuePair pair : params) {
        if (first)
            first = false;/*  w  w  w  .  j  ava  2s. c  om*/
        else
            result.append("&");

        result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
    }

    return result.toString();
}

From source file:Main.java

public static String getPostDataString(HashMap<String, String> params) {
    try {/*  w  ww.  j  av a 2  s  .c  o m*/
        StringBuilder result = new StringBuilder();
        boolean first = true;

        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (first)
                first = false;
            else
                result.append("&");

            result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            result.append("=");
            if (entry.getValue() == null) {
                result.append(URLEncoder.encode("", "UTF-8"));
            } else {
                result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
            }

        }
        return result.toString();
    } catch (UnsupportedEncodingException e) {
        System.out.print(e.getMessage());
    }
    return "";
}

From source file:Main.java

public static String encodeURL(String raw, String charsetName) {
    if (raw == null) {
        return null;
    }//from   ww w. j  ava2  s .c o  m
    try {
        if (TextUtils.isEmpty(charsetName)) {
            charsetName = Charset.defaultCharset().displayName();
        }
        return URLEncoder.encode(raw, charsetName);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String utf8Encode(String str, String defultReturn) {
    if (!isEmpty(str) && str.getBytes().length != str.length()) {
        try {// ww  w.j av a 2  s.  c  o  m
            return URLEncoder.encode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return defultReturn;
        }
    }
    return str;
}