Provided I have a java.net.URL object, pointing to let's say
http://example.com/myItems
or http://example.com/myItems/
Is there some helper somewhere to append some relative URL to this?
For instance append ./myItemId
or myItemId
to get :
http://example.com/myItems/myItemId
This one does not need any extra libs or code and gives the desired result:
This prints:
The accepted answer only works if there is no path after the host (IMHO the accepted answer is wrong)
I had some difficulty with the encoding of URI's. Appending was not working for me because it was of a content:// type and it was not liking the "/". This solution assumes no query, nor fragment(we are working with paths after all):
Kotlin code:
Here is a helper function I've written to add to the url path:
I've searched far and wide for an answer to this question. The only implementation I can find is in the Android SDK: Uri.Builder. I've extracted it for my own purposes.
This is where I found the source.
In conjunction with Apache URIBuilder, this is how I'm using it:
builder.setPath(appendSegmentToPath(builder.getPath(), segment));
For android make sure you use
.appendPath()
fromandroid.net.Uri
My solution based on twhitbeck answer:
Test:
Gist: https://gist.github.com/enginer/230e2dc2f1d213a825d5