Here you can find the source of encodeIfNeeded(String uri)
public static String encodeIfNeeded(String uri) throws UnsupportedEncodingException
//package com.java2s; //License from project: Open Source License import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; public class Main { public static String encodeIfNeeded(String uri) throws UnsupportedEncodingException { String decodedUri = URLDecoder.decode(uri, "UTF-8"); // It is necessary to encode it if (uri.compareTo(decodedUri) == 0) return URLEncoder.encode(uri, "UTF-8"); // If was already encoded else/* ww w.j a va2 s . co m*/ return uri; } }