Skip to content

Instantly share code, notes, and snippets.

@hkurokawa
Last active March 29, 2016 01:00
Show Gist options
  • Select an option

  • Save hkurokawa/1fdda8ee053940fe2816 to your computer and use it in GitHub Desktop.

Select an option

Save hkurokawa/1fdda8ee053940fe2816 to your computer and use it in GitHub Desktop.
A utility method to encode only the path segments of the given URL on Android
import android.net.Uri;
import android.util.Pair;
import java.util.List;
public final class UrlUtil {
static String encodePathSegments(String url) {
final Uri uri = Uri.parse(url);
final List<String> pathSegments = uri.getPathSegments();
final Uri.Builder builder = uri.buildUpon().path("");
for (String s : pathSegments) {
builder.appendEncodedPath(Uri.encode(s));
}
return builder.build().toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment