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 encodeUriSegment(String segment) {
    try {//w  ww .  ja v a  2  s .  co  m
        return URLEncoder.encode(segment, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new IllegalStateException(e);
    }
}

From source file:Main.java

public static String encodeParameters(Map<String, String> params) throws UnsupportedEncodingException {
    StringBuilder body = new StringBuilder();
    Iterator<Entry<String, String>> it = params.entrySet().iterator();
    boolean firstElement = true;
    while (it.hasNext()) {
        Map.Entry<String, String> pair = it.next();
        if (firstElement) {
            firstElement = false;/*  w  ww.ja  v a2  s .  co m*/
        } else {
            body.append("&");
        }
        body.append(URLEncoder.encode(pair.getKey(), CHARSET) + "="
                + ((pair.getValue() == null) ? "null" : URLEncoder.encode(pair.getValue(), CHARSET)));
    }
    return body.toString();
}

From source file:test.LocationCrawler.java

private static String ProcessLocationRequest(String QueryItem) {
    try {/*  w  w w .  ja v a2  s . c  o  m*/
        QueryItem = URLEncoder.encode(QueryItem, "ISO-8859-1");
        String URLStr = String.format(
                "https://maps.googleapis.com/maps/api/geocode/json?" + "address=%s&sensor=false&key=%s",
                QueryItem,
                //"AIzaSyAOkEu7MBxUj4t2pBq1GZ-0Td7cf7bOKTg");
                //"AIzaSyCvzTx408371P7CtoXN8BejAAmBa0NUZbc");
                "AIzaSyAIyQ3XaHrC9TQVMIs65IrwzREtnzqZRKw");
        URL url = new URL(URLStr);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        if (connection.getResponseCode() == 200) {
            InputStream is = connection.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String sb = "";
            String str;

            while ((str = br.readLine()) != null) {
                sb += (str);
            }

            br.close();

            return sb;
        } else {
            return "";
        }
    } catch (Exception e) {
        return "";
    }
}

From source file:Main.java

public static String encodeUrl(Map<String, String> param) {
    if (param == null) {
        return "";
    }/*from   w w w . j  a  v a  2  s  .com*/
    StringBuilder sb = new StringBuilder();
    Set<String> keys = param.keySet();
    boolean first = true;

    for (String key : keys) {
        String value = param.get(key);
        //pain...EditMyProfileDao params' values can be empty
        if (value != null || key.equals("description") || key.equals("url")) {
            if (first) {
                first = false;
            } else {
                sb.append("&");
            }
            try {
                sb.append(URLEncoder.encode(key, "UTF-8")).append("=")
                        .append(URLEncoder.encode(param.get(key), "UTF-8"));
            } catch (UnsupportedEncodingException e) {

            }
        }

    }

    return sb.toString();
}

From source file:Main.java

/**
 * Converts <code>params</code> into an application/x-www-form-urlencoded encoded string.
 *//*from   ww  w. ja  v  a 2 s.  c o  m*/
public static byte[] encodeParamsToBytes(Map<String, String> params, String paramsEncoding) {
    StringBuilder encodedParams = new StringBuilder();
    try {
        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 encodedParams.toString().getBytes(paramsEncoding);
    } catch (UnsupportedEncodingException uee) {
        throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
    }
}

From source file:Main.java

/**
 * Converts <code>params</code> into an application/x-www-form-urlencoded encoded string.
 *///from  ww  w .j  a  v  a 2  s.  c  o m
public static String encodeParamsToStr(Map<String, String> params, String paramsEncoding) {
    StringBuilder encodedParams = new StringBuilder();
    try {
        int i = 0;
        for (Map.Entry<String, String> entry : params.entrySet()) {
            encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
            encodedParams.append('=');
            encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
            if (i != params.size() - 1) {
                encodedParams.append('&');
            }
            i++;
        }
        return encodedParams.toString();
    } catch (UnsupportedEncodingException uee) {
        throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
    }
}

From source file:conroller.TankDepthDetailsController.java

public static String addTankDepthDetails(TankDepthDetailsModel model) throws IOException {
    phpConnection.setConnection(//from  w  w w.j  ava  2  s  . c o  m
            "http://itmahaweliauthority.net23.net/MahaweliAuthority/TankLevel/InsertTankDepthDetails.php");
    String data = URLEncoder.encode("time", "UTF-8") + "=" + URLEncoder.encode(model.getTime(), "UTF-8") + "&"
            + URLEncoder.encode("day", "UTF-8") + "=" + URLEncoder.encode(model.getDay(), "UTF-8") + "&"
            + URLEncoder.encode("tankName", "UTF-8") + "=" + URLEncoder.encode(model.getTankName(), "UTF-8")
            + "&" + URLEncoder.encode("tankLevel", "UTF-8") + "="
            + URLEncoder.encode(model.getTankLevel(), "UTF-8") + "&" + URLEncoder.encode("predict", "UTF-8")
            + "=" + URLEncoder.encode(model.getPredict(), "UTF-8");
    phpConnection.sendData(data);
    return phpConnection.readData();

}

From source file:conroller.TankController.java

public static String addTank(TankModel model) throws IOException {
    phpConnection.setConnection(/*  ww w  .j  a  v a2  s.c  om*/
            "http://itmahaweliauthority.net23.net/MahaweliAuthority/TankPHPfiles/InsertTank.php");
    String data = URLEncoder.encode("tankName", "UTF-8") + "=" + URLEncoder.encode(model.getTankName(), "UTF-8")
            + "&" + URLEncoder.encode("tankEngineer", "UTF-8") + "="
            + URLEncoder.encode(model.getTankEngineer(), "UTF-8") + "&"
            + URLEncoder.encode("tankDetails", "UTF-8") + "="
            + URLEncoder.encode(model.getTankDetails(), "UTF-8") + "&" + URLEncoder.encode("maxDepth", "UTF-8")
            + "=" + URLEncoder.encode(Integer.toString(model.getMaxDepth()), "UTF-8");
    phpConnection.sendData(data);
    return phpConnection.readData();

}

From source file:Main.java

/**
 * encoded in utf-8, if exception, return defultReturn
 *
 * @param str// w w w. j  a v  a 2s .co  m
 * @param defultReturn
 * @return
 */
public static String utf8Encode(String str, String defultReturn) {
    if (!isEmpty(str) && str.getBytes().length != str.length()) {
        try {
            return URLEncoder.encode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return defultReturn;
        }
    }
    return str;
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.edit.listing.ListingControllerWebUtils.java

public static synchronized String formatIndividualLink(Individual ind) {
    try {//from   w w w .  jav  a  2s  . c om
        System.out.println(ind.getURI());
        String nameStr = (ind.getName() != null) ? ind.getName() : ind.getURI();
        return "<a href=\"entityEdit?uri=" + URLEncoder.encode(ind.getURI(), "UTF-8") + "\">" + nameStr
                + "</a>";
    } catch (NullPointerException npe) {
        return "?";
    } catch (UnsupportedEncodingException e) {
        return ind.getName();
    }
}