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 encode(Map<String, String> params, String encoding) {
    StringBuilder encodedParams = new StringBuilder();

    try {//from www . ja  va2  s . co  m
        Set uee = params.entrySet();
        int size = uee.size();
        int index = 0;
        Iterator iterator = uee.iterator();

        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            encodedParams.append(URLEncoder.encode((String) entry.getKey(), encoding));
            encodedParams.append('=');
            encodedParams.append(URLEncoder.encode((String) entry.getValue(), encoding));
            ++index;
            if (index < size) {
                encodedParams.append('&');
            }
        }

        return encodedParams.toString();
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Encoding not supported: " + encoding, e);
    }
}

From source file:Main.java

static String jsonToQueryString(JSONObject jsonObj) {
    StringBuilder sb = new StringBuilder();

    try {//from ww  w. j a  va2  s.  c o m

        Iterator<String> i = (Iterator<String>) jsonObj.keys();
        while (i.hasNext()) {
            String key = (String) i.next();
            Object value;
            try {
                value = jsonObj.get(key);
            } catch (JSONException e) {
                value = "";
            }
            sb.append(URLEncoder.encode(key, "UTF-8"));
            sb.append("=");
            sb.append(URLEncoder.encode(value.toString(), "UTF-8"));
            sb.append("&");
        }
    } catch (UnsupportedEncodingException e) {
        System.out.println(e);
    } catch (Exception e) {
    }

    return sb.substring(0, sb.length() - 1);
}

From source file:Main.java

public static String buildEncodedQueryString(HashMap<String, String> requestParams) {
    String queryString = "?";
    if (requestParams == null) {
        return null;
    }/* w  ww . j a va  2s.c o  m*/
    Iterator<Entry<String, String>> it = requestParams.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, String> pairs = (Map.Entry<String, String>) it.next();
        try {
            queryString += URLEncoder.encode(pairs.getKey().toString(), "UTF-8") + "="
                    + URLEncoder.encode(pairs.getValue().toString(), "UTF-8") + "&";
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if (queryString.length() > 0)
        queryString = queryString.substring(0, queryString.length() - 1);
    return queryString;
}

From source file:edu.northwestern.bioinformatics.studycalendar.core.editors.EditorUtils.java

public static String getEncodedString(String str) {
    try {//from  w w  w.ja va 2 s  . co m
        return URLEncoder.encode(str, CHAR_ENCODE);
    } catch (UnsupportedEncodingException e) {
        throw new StudyCalendarError("Unsupported character encoding", e);
    }
}

From source file:Main.java

public static String getEncoder(String str) {
    try {/* w  w  w  .j  av a  2s .  c o  m*/
        return URLEncoder.encode(str, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static void composeEmail(Context context, String email, String subject, String body) {
    try {// www.  j  a  va2s.  c om
        String url = "mailto:";
        if (email != null) {
            url += email;
        }
        url += "?";

        if (subject != null) {
            String subjectEncoded = URLEncoder.encode(subject, "UTF-8");
            subjectEncoded = subjectEncoded.replace("+", "%20");
            url += "subject=" + subjectEncoded;
        }

        if (body != null) {
            if (subject != null) {
                url += "&";
            }
            String bodyEncoded = URLEncoder.encode(body, "UTF-8");
            bodyEncoded = bodyEncoded.replace("+", "%20");
            url += "body=" + bodyEncoded;
        }

        viewURL(context, url);
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:Main.java

public static String encodeParam(Map<String, String> params) {
    if (params == null)
        return "";

    StringBuilder sb = new StringBuilder();

    for (String key : params.keySet()) {
        Object val = params.get(key);
        if (sb.length() != 0)
            sb.append("&");

        if (val instanceof Long[]) {
            Long[] longval = (Long[]) val;

            try {
                for (int i = 0; i < longval.length; i++) {
                    String valStr = (val == null) ? "" : URLEncoder.encode(longval[i].toString(), "utf-8");

                    if (i != longval.length - 1)
                        sb.append(key + "=" + valStr + "&");
                    else
                        sb.append(key + "=" + valStr);
                }//from w  w  w  .j  av  a 2  s. c  om

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        } else {
            try {
                String valStr = (val == null) ? "" : URLEncoder.encode(val.toString(), "utf-8");
                sb.append(key + "=" + valStr);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

    }
    return sb.toString();
}

From source file:be.geecko.QuickLyric.lyrics.LyricsWiki.java

public static Lyrics fromMetaData(String artist, String song) {
    if ((artist == null) || (song == null))
        return new Lyrics(Lyrics.ERROR);
    String encodedArtist;//  w w w  .j a v  a2 s  .  c o  m
    String encodedSong;
    URL url;
    try {
        encodedArtist = URLEncoder.encode(artist, "UTF-8");
        encodedSong = URLEncoder.encode(song, "UTF-8");
        JSONObject json = new JSONObject(
                getUrlAsString(new URL(String.format(baseUrl, encodedArtist, encodedSong))).replace("song = ",
                        ""));
        url = new URL(json.getString("url"));
        artist = json.getString("artist");
        song = json.getString("song");
    } catch (JSONException | IOException e) {
        e.printStackTrace();
        return new Lyrics(Lyrics.ERROR);
    }
    return fromURL(url.toExternalForm(), artist, song);
}

From source file:Main.java

public static String urlencode(String[] params) {
    if (params.length % 2 != 0) {
        throw new IllegalArgumentException("Params must have an even number of elements.");
    }/*from ww w.  j a  v  a  2 s  .co  m*/

    String result = "";
    try {
        boolean firstTime = true;
        for (int i = 0; i < params.length; i += 2) {
            if (params[i + 1] != null) {
                if (firstTime) {
                    firstTime = false;
                } else {
                    result += "&";
                }
                result += URLEncoder.encode(params[i], "UTF-8") + "="
                        + URLEncoder.encode(params[i + 1], "UTF-8");
            }
        }
        result.replace("*", "%2A");
    } catch (UnsupportedEncodingException e) {
        return null;
    }

    return result;
}

From source file:de.dfki.resc28.ole.bootstrap.Util.java

public static String urlEncoded(String text) {
    try {//from   w w w.  j av a2  s.c om
        return URLEncoder.encode(text, "UTF-8");
    } catch (UnsupportedEncodingException ex) {
        System.err.format("Could not convert string to UTF-8: %s%n", ex);
        return text;
    }
}