List of usage examples for java.net URLEncoder encode
public static String encode(String s, Charset charset)
From source file:Main.java
/** Builds a parameter string based on the given parameters, separated by given delimiter, and optionally quoted values */ public static String buildParameterString(List<Pair<String, String>> parameters, String delimiter, boolean quote) throws UnsupportedEncodingException { if (parameters == null || parameters.isEmpty()) return ""; StringBuilder builder = new StringBuilder(); for (int i = 0; i < parameters.size(); i++) { String key = parameters.get(i).first; String value = parameters.get(i).second; // skip 'null' values if (value == null) continue; String a = URLEncoder.encode(key, "UTF-8"); String b = URLEncoder.encode(value, "UTF-8"); builder.append(a + "="); // append key + "=" if (quote) builder.append("\""); // optionally append quote builder.append(b); // append value if (quote) builder.append("\""); // optionally append quote if (i != parameters.size() - 1) builder.append(delimiter);//from w w w .ja v a2 s . co m } return builder.toString(); }
From source file:Main.java
public static int sendMessage(String auth_token, String registrationId, String message) throws IOException { StringBuilder postDataBuilder = new StringBuilder(); postDataBuilder.append(PARAM_REGISTRATION_ID).append("=").append(registrationId); postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=").append("0"); postDataBuilder.append("&").append("data.payload").append("=") .append(URLEncoder.encode("Lars war hier", UTF8)); byte[] postData = postDataBuilder.toString().getBytes(UTF8); // Hit the dm URL. URL url = new URL("https://android.clients.google.com/c2dm/send"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true);/*from www .j a v a2 s . co m*/ conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); conn.setRequestProperty("Content-Length", Integer.toString(postData.length)); conn.setRequestProperty("Authorization", "GoogleLogin auth=" + auth_token); OutputStream out = conn.getOutputStream(); out.write(postData); out.close(); int responseCode = conn.getResponseCode(); return responseCode; }
From source file:Main.java
private static final String encodeURIComponent(final String s) { String result;/*from w w w. j a v a2s . c o m*/ try { result = URLEncoder.encode(s, "UTF-8").replaceAll("\\+", "%20").replaceAll("\\%21", "!") .replaceAll("\\%27", "'").replaceAll("\\%28", "(").replaceAll("\\%29", ")") .replaceAll("\\%7E", "~"); } catch (UnsupportedEncodingException e) { result = s; } return result; }
From source file:Main.java
public static String encodeUrl(Map<String, String> param) { if (param == null) { return ""; }/*from w ww . j a v a 2 s . c o m*/ 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 (!TextUtils.isEmpty(value) || 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
public static String encode(CharSequence target) { if (target == null) { return ""; }/*from w w w . jav a 2 s. co m*/ String result = target.toString(); try { result = URLEncoder.encode(result, "UTF8"); } catch (UnsupportedEncodingException ex) { ex.printStackTrace(); } return result; }
From source file:Main.java
public static String getRequestParamValue(Object obj, String charset) { if (obj == null) { return ""; }/* ww w .j a v a2 s . co m*/ String value; if (obj instanceof List) { StringBuilder sb = new StringBuilder(); if (obj != null) { for (Object o : (List<?>) obj) { if (o != null) { sb.append(o.toString()); sb.append(','); } } } if (sb.length() > 0) { sb.deleteCharAt(sb.length() - 1); } value = sb.toString(); } else { value = obj.toString(); } try { return URLEncoder.encode(value, charset); } catch (UnsupportedEncodingException e) { return value; } }
From source file:Main.java
public static String encode(final String content, final String encoding) { try {/*from www . j a v a2s . co m*/ return URLEncoder.encode(content, encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET); } catch (UnsupportedEncodingException problem) { throw new IllegalArgumentException(problem); } }
From source file:Main.java
public static String encodeQuery(String url) { Uri uri = Uri.parse(url);//from w w w .jav a 2 s . c o m try { String query = uri.getQuery(); String encodedQuery = query != null ? URLEncoder.encode(query, "UTF-8") : null; URI tmp = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), null, uri.getFragment()); return tmp + (encodedQuery != null && encodedQuery.length() > 0 ? "?" + encodedQuery : ""); } catch (UnsupportedEncodingException ignore) { } catch (URISyntaxException ignore) { } return uri.toString(); }
From source file:Main.java
public static String urlEncode(String s) { try {/*from w ww . j ava 2s. c o m*/ return URLEncoder.encode(s, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String urlEncode(String str) { try {// w ww .j a va2 s . co m return URLEncoder.encode(str, UTF8); } catch (UnsupportedEncodingException ex) { return str; } }