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:com.esofthead.mycollab.common.UrlEncodeDecoder.java

/**
 * //from ww w. j  av a2s . com
 * @param str
 * @return
 */
public static String encode(String str) {
    try {
        if (StringUtils.isBlank(str)) {
            return "";
        }
        return URLEncoder.encode(new String(Base64.encodeBase64URLSafe(str.getBytes("UTF-8")), "UTF-8"),
                "UTF-8");
    } catch (UnsupportedEncodingException ex) {
        throw new MyCollabException(ex);
    }
}

From source file:jfix.util.Urls.java

/**
 * Translates given string into application/x-www-form-urlencoded format
 * using a specific encoding scheme.//from   w  w  w  .  j  a va  2s. c o m
 */
public static String encode(String str, String enc) {
    try {
        return URLEncoder.encode(str, enc);
    } catch (Exception e) {
        return str;
    }
}

From source file:com.fastbootmobile.encore.api.gimages.GoogleImagesClient.java

public static String getImageUrl(String query) throws IOException, JSONException, RateLimitException {
    if (query == null) {
        Log.e(TAG, "Null query!");
        return null;
    }/*www. ja  v a 2s.com*/

    String queryUrl = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q="
            + URLEncoder.encode(query, "UTF-8") + "&imgsz=large";

    JSONObject obj = JsonGet.getObject(queryUrl, "", true);
    JSONArray results = obj.getJSONObject("responseData").getJSONArray("results");

    if (results.length() > 0) {
        return URLDecoder.decode(results.getJSONObject(0).getString("url"), "UTF-8");
    } else {
        return null;
    }
}

From source file:com.weibo.api.motan.util.StringTools.java

public static String urlEncode(String value) {
    if (StringUtils.isEmpty(value)) {
        return "";
    }//from  ww  w . j a va 2  s  .com
    try {
        return URLEncoder.encode(value, MotanConstants.DEFAULT_CHARACTER);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:com.alacoder.lion.common.utils.StringTools.java

public static String urlEncode(String value) {
    if (StringUtils.isEmpty(value)) {
        return "";
    }//w  w  w.j ava2  s  .  c o  m
    try {
        return URLEncoder.encode(value, LionConstants.DEFAULT_CHARACTER);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:com.kth.common.utils.etc.URLCoderUtil.java

/** URL? URL Encode. */
public static String encode(final String content, final String encoding) {
    try {//  w  w  w. j  av a  2  s  .c  o  m
        return URLEncoder.encode(content, encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET);
    } catch (UnsupportedEncodingException problem) {
        throw new IllegalArgumentException(problem);
    }
}

From source file:com.jivesoftware.sdk.utils.JiveSDKUtils.java

@Nonnull
public static String encodeUrl(@Nonnull String url) {
    try {/*from  ww  w  .j a v  a2s . c o m*/
        return URLEncoder.encode(url, "UTF-8");
    } catch (UnsupportedEncodingException uee) {
        //TODO: LOGGER
        System.err.println("Failed encoding URL using UTF-8 charset" + uee.getMessage());
        //noinspection deprecation
        return URLEncoder.encode(url);
    }
}

From source file:com.geecko.QuickLyric.lyrics.Bollywood.java

public static ArrayList<Lyrics> search(String query) {
    ArrayList<Lyrics> results = new ArrayList<>();
    String searchUrl = "http://quicklyric.netii.net/bollywood/search.php?q=%s";
    try {/*from  w w  w  .  j a va  2  s .c o  m*/
        String jsonText;
        jsonText = Net.getUrlAsString(String.format(searchUrl, URLEncoder.encode(query, "utf-8")));
        JSONObject jsonResponse = new JSONObject(jsonText);
        JSONArray lyricsResults = jsonResponse.getJSONArray("lyrics");
        for (int i = 0; i < lyricsResults.length(); ++i) {
            JSONObject lyricsResult = lyricsResults.getJSONObject(i);
            JSONArray tags = lyricsResult.getJSONArray("tags");
            Lyrics lyrics = new Lyrics(Lyrics.SEARCH_ITEM);
            lyrics.setTitle(lyricsResult.getString("name"));
            for (int j = 0; i < tags.length(); ++j) {
                JSONObject tag = tags.getJSONObject(j);
                if (tag.getString("tag_type").equals("Singer")) {
                    lyrics.setArtist(tag.getString("name").trim());
                    break;
                }
            }
            lyrics.setURL("http://quicklyric.netii.net/bollywood/get.php?id=" + lyricsResult.getInt("id"));
            results.add(lyrics);
        }
    } catch (IOException | JSONException e) {
        e.printStackTrace();
    }
    return results;
}

From source file:cn.edu.zjnu.acm.judge.exception.GlobalExceptionHandler.java

public static String unauthorized(HttpServletRequest request) throws IOException {
    String url = (String) request.getAttribute(JudgeHandlerInterceptor.BACK_URL_ATTRIBUTE_NAME);
    return url == null ? "redirect:/login" : "redirect:/login?url=" + URLEncoder.encode(url, "UTF-8");
}

From source file:ch.cyberduck.core.URIEncoder.java

/**
 * URL encode a path/*from   w w w.j a v a 2s. c  o m*/
 *
 * @param p Path
 * @return URI encoded
 * @see java.net.URLEncoder#encode(String, String)
 */
public static String encode(final String p) {
    try {
        final StringBuilder b = new StringBuilder();
        final StringTokenizer t = new StringTokenizer(p, "/");
        if (!t.hasMoreTokens()) {
            return p;
        }
        if (StringUtils.startsWith(p, String.valueOf(Path.DELIMITER))) {
            b.append(Path.DELIMITER);
        }
        while (t.hasMoreTokens()) {
            b.append(URLEncoder.encode(t.nextToken(), "UTF-8"));
            if (t.hasMoreTokens()) {
                b.append(Path.DELIMITER);
            }
        }
        if (StringUtils.endsWith(p, String.valueOf(Path.DELIMITER))) {
            b.append(Path.DELIMITER);
        }
        // Because URLEncoder uses <code>application/x-www-form-urlencoded</code> we have to replace these
        // for proper URI percented encoding.
        return b.toString().replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
    } catch (UnsupportedEncodingException e) {
        return p;
    }
}