提交 ce6e0d17 编写于 作者: S Syed Seth 提交者: Jake Wharton

`URL` overload for `Retrofit.Builder#baseUrl`

Functionally does nothing, just somewhat cleaner to be able to do `baseUrl(URL)` than `baseUrl(URL#toString());` if handling a `URL` rather than `String`.
Used `HttpUrl.get(String)` as `HttpUrl.get(URL)` returns null instead of throwing an exception.
上级 81a6193a
......@@ -21,6 +21,7 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
......@@ -449,6 +450,16 @@ public final class Retrofit {
return this;
}
/**
* Set the API base URL.
*
* @see #baseUrl(HttpUrl)
*/
public Builder baseUrl(URL baseUrl) {
checkNotNull(baseUrl, "baseUrl == null");
return baseUrl(HttpUrl.get(baseUrl.toString()));
}
/**
* Set the API base URL.
*
......
......@@ -20,6 +20,8 @@ import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Set;
......@@ -749,6 +751,14 @@ public final class RetrofitTest {
assertThat(retrofit.baseUrl()).isSameAs(url);
}
@Test public void baseJavaUrlPropagated() throws MalformedURLException {
URL url = new URL("http://example.com/");
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.build();
assertThat(retrofit.baseUrl()).isEqualTo(HttpUrl.get("http://example.com/"));
}
@Test public void clientNullThrows() {
try {
new Retrofit.Builder().client(null);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册