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.wizard.routinemobile.baiduapi.BaiduAPI.java

public static WeatherResult getWeater(WizardHTTP wc, String city) throws IOException, JSONException {
    wc.setCharset("utf-8");
    String retstr = wc.httpGet("http://api.map.baidu.com/telematics/v3/weather" + "?location="
            + URLEncoder.encode(city, "utf-8") + "&output=json&ak=" + BaiduAPI.API_KEY);
    JSONObject json = new JSONObject(retstr);
    int errno = json.getInt("error");
    if (errno != 0)
        return new WeatherResult(errno, json.getString("status"), "", "", "");
    JSONObject weatherdata = json.getJSONArray("results").getJSONObject(0).getJSONArray("weather_data")
            .getJSONObject(0);/*from   ww  w . j a v  a  2  s.  c om*/
    String weather = weatherdata.getString("weather");
    String wind = weatherdata.getString("wind");
    String temperature = weatherdata.getString("temperature");
    return new WeatherResult(0, "", weather, wind, temperature);
}

From source file:com.graphhopper.api.WebHelper.java

public static String encodeURL(String str) {
    try {//from  ww w .  ja  v  a 2s.  c  o  m
        return URLEncoder.encode(str, "UTF-8");
    } catch (Exception _ignore) {
        return str;
    }
}

From source file:Main.java

/**
 * Combined the namespace and the local name back together.
 * @param ns the namespace uri//  w  w w .jav a2  s. c o m
 * @param lname the local name
 * @param joiner the join string, e.g. <code>#</code>
 * @return the joined uri
 */
public static String unsplit(String ns, String lname, String joiner) {
    if (!ns.endsWith(joiner)) {
        ns += joiner;
    }
    try {
        return ns + URLEncoder.encode(lname, charset);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("You don't support " + charset + "?");
    }
}

From source file:com.amazon.s3.util.HttpUtils.java

public static String urlEncode(String value, boolean path) {
    if (value == null)
        return "";

    try {//from www . j  a  v  a2 s  .  c  o  m
        String encoded = URLEncoder.encode(value, DEFAULT_ENCODING).replace("+", "%20").replace("*", "%2A")
                .replace("%7E", "~");
        if (path) {
            encoded = encoded.replace("%2F", "/");
        }

        return encoded;
    } catch (UnsupportedEncodingException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:de.micromata.genome.gwiki.utils.WebUtils.java

public static String encodeUrlParam(String value) {
    if (StringUtils.isEmpty(value) == true) {
        return "";
    }/*from  w ww .java  2 s . c om*/

    try {
        return URLEncoder.encode(value, "UTF-8");
    } catch (UnsupportedEncodingException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.thoughtworks.go.util.UrlUtil.java

public static String encodeInUtf8(String url) {
    String[] parts = url.split("/");
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < parts.length; i++) {
        String part = parts[i];/*from  w ww  .  j a  v a2s.c  om*/
        try {
            builder.append(URLEncoder.encode(part, UTF_8));
        } catch (UnsupportedEncodingException e) {
            bomb(e);
        }
        if (i < parts.length - 1) {
            builder.append('/');
        }
    }
    if (url.endsWith("/")) {
        builder.append('/');
    }
    return builder.toString();
}

From source file:net.foreworld.util.RestUtil.java

public static String genSignature(String data, String key) {
    byte[] byteHMAC = null;
    try {/* w  w w .  j av a  2 s  .c o m*/
        Mac mac = Mac.getInstance(ALGORITHM);
        SecretKeySpec spec = new SecretKeySpec(key.getBytes(), ALGORITHM);
        mac.init(spec);
        byteHMAC = mac.doFinal(data.toLowerCase(Locale.getDefault()).getBytes());
    } catch (InvalidKeyException ignore) {
        return null;
    } catch (NoSuchAlgorithmException ignore) {
        return null;
    } // END

    if (null == byteHMAC)
        return null;

    // String code = new BASE64Encoder().encode(byteHMAC);
    String code = Base64.encodeBase64String(byteHMAC);

    try {
        return URLEncoder.encode(code, ENC);
    } catch (UnsupportedEncodingException ignore) {
    }
    return null;
}

From source file:Main.java

public static String urlencode(String paramString1, String paramString2) {
    if (paramString1 == null)
        return "";
    try {/*from   www. jav  a  2  s .  c o m*/
        String str = URLEncoder.encode(paramString1, paramString2);
        return str;
    } catch (UnsupportedEncodingException localUnsupportedEncodingException) {
        throw new RuntimeException(localUnsupportedEncodingException.getMessage(),
                localUnsupportedEncodingException);
    }
}

From source file:Main.java

public static String encodeUrl(String url) {
    Uri uri = Uri.parse(url);//from  w  w w .j a v  a2 s.c o  m

    try {
        Map<String, List<String>> splitQuery = splitQuery(uri);
        StringBuilder encodedQuery = new StringBuilder();
        for (String key : splitQuery.keySet()) {
            for (String value : splitQuery.get(key)) {
                if (encodedQuery.length() > 0) {
                    encodedQuery.append("&");
                }
                encodedQuery.append(key + "=" + URLEncoder.encode(value, "UTF-8"));
            }
        }
        String queryString = encodedQuery != null && encodedQuery.length() > 0 ? "?" + encodedQuery : "";

        URI baseUri = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), null, uri.getFragment());
        return baseUri + queryString;
    } catch (UnsupportedEncodingException ignore) {
    } catch (URISyntaxException ignore) {
    }

    return uri.toString();
}

From source file:com.ibm.watson.developer_cloud.util.RequestUtil.java

/**
 * Encode./*from   w  ww .  j a  va2s. c  o  m*/
 * 
 * @param content the content
 * @return the string
 */
public static String encode(String content) {
    try {
        return URLEncoder.encode(content, "UTF-8");
    } catch (final UnsupportedEncodingException e) {
        return null;
    }
}