Extract base url from full url
java.net.URL doc can help you to figure out how to "Extract base url from full url"
Preliminary note:
One should pay attention to this important note from the URL javadoc page:
"Note, the URI class does perform escaping of its component fields in certain circumstances. The recommended way to manage the encoding and decoding of URLs is to use URI, and to convert between these two classes using toURI() and URI.toURL()."
Self Explanatory Unit tests:
Here are 2 simlple unit tests to illustrate the 'base Url' concept
package com.elementique.web;
import org.junit.Test;
import java.net.URI;
import java.net.URL;
import static org.junit.Assert.assertEquals;
public class UrlTest {
@Test
public void baseUrlAuthority() throws Exception {
URL url = URI.create("http://username:password@host1:8080/directory/file?query#ref").toURL();
assertEquals("http", url.getProtocol()); // protocol is also known as 'scheme'
assertEquals("username:password@host1:8080", url.getAuthority());
String baseUrlAuthority= url.getProtocol() + "://" + url.getAuthority();
assert (url.toString().startsWith(baseUrlAuthority));
}
@Test
public void baseUrlHostAndDefaultPort() throws Exception {
URL url = URI.create("http://host2/a/b/c").toURL();
assertEquals(-1, url.getPort()); // -1 as port is not defined in this case
String baseUrlHostAndDefaultPort= url.getProtocol() + "://" + url.getHost();
assert (url.toString().startsWith(baseUrlHostAndDefaultPort));
}
}
Extracting base URL:
So, "extracting base Url from full url" can be done like this:
return url.getProtocol()+"://"+url.getAuthority()+"/"
Or this, if you do NOT want the username/password part
if(url.getPort() == -1){ // port is not
return url.getProtocol()+"://"+url.getHost()+"/";
} else {
return url.getProtocol()+"://"+url.getHost()+":"+url.getPort()+"/";
}
Implementation:
Here is an implementation that does that (based on getAuthority() ):
public static String getBaseUrl(String urlString)
{
if(urlString == null)
{
return null;
}
try
{
URL url = URI.create(urlString).toURL();
return url.getProtocol() + "://" + url.getAuthority() + "/";
}
catch (Exception e)
{
return null;
}
}
Note:
Remove the trailing "/" is you don't want it
What about:
import java.net.URL;
import java.net.MalformedURLException;
try
{
URL url = new URL("http://test.example.com/abcd/test.html");
String baseUrl = url.getProtocol() + "://" + url.getHost();
}
catch (MalformedURLException e)
{
// do something
}
There's nothing in the Android URI class that gives you the base URL directly- As gnuf suggests, you'd have to construct it as the protocol + getHost().
The string parsing way might be easier and let you avoid stuffing everything in a try/catch block. Just take a substring of the URL, everything from the beginning of the string to the third "/" symbol.