Skip to content

Commit

Permalink
Add OkHttp-specific client when its present on the classpath.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed May 9, 2013
1 parent 360eedb commit bdaf792
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<android.platform>16</android.platform>
<gson.version>2.2.2</gson.version>
<javax.inject.version>1</javax.inject.version>
<okhttp.version>1.0.1</okhttp.version>

<!-- Test Dependencies -->
<junit.version>4.10</junit.version>
Expand Down Expand Up @@ -105,6 +106,11 @@
<artifactId>android</artifactId>
<version>${android.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions retrofit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<artifactId>android</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>junit</groupId>
Expand Down
33 changes: 31 additions & 2 deletions retrofit/src/main/java/retrofit/http/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import retrofit.http.android.AndroidApacheClient;
import retrofit.http.android.MainThreadExecutor;
import retrofit.http.client.Client;
import retrofit.http.client.OkClient;
import retrofit.http.client.UrlConnectionClient;

import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
Expand All @@ -36,6 +37,7 @@ private static Platform findPlatform() {
Converter defaultConverter() {
return new GsonConverter(new Gson());
}

abstract Client.Provider defaultClient();
abstract Executor defaultHttpExecutor();
abstract Executor defaultCallbackExecutor();
Expand All @@ -44,7 +46,12 @@ Converter defaultConverter() {
/** Provides sane defaults for operation on the JVM. */
private static class Base extends Platform {
@Override Client.Provider defaultClient() {
final Client client = new UrlConnectionClient();
final Client client;
if (hasOkHttpOnClasspath()) {
client = OkClientInstantiator.instantiate();
} else {
client = new UrlConnectionClient();
}
return new Client.Provider() {
@Override public Client get() {
return client;
Expand Down Expand Up @@ -82,7 +89,9 @@ private static class Base extends Platform {
private static class Android extends Platform {
@Override Client.Provider defaultClient() {
final Client client;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
if (hasOkHttpOnClasspath()) {
client = OkClientInstantiator.instantiate();
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
client = new AndroidApacheClient();
} else {
client = new UrlConnectionClient();
Expand Down Expand Up @@ -119,4 +128,24 @@ private static class Android extends Platform {
};
}
}

/** Determine whether or not OkHttp is present on the runtime classpath. */
private static boolean hasOkHttpOnClasspath() {
try {
Class.forName("com.squareup.okhttp.OkHttpClient");
return true;
} catch (ClassNotFoundException e) {
return false;
}
}

/**
* Indirection for OkHttp class to prevent VerifyErrors on Android 2.0 and earlier when the
* dependency is not present..
*/
private static class OkClientInstantiator {
static Client instantiate() {
return new OkClient();
}
}
}
24 changes: 24 additions & 0 deletions retrofit/src/main/java/retrofit/http/client/OkClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2013 Square, Inc.
package retrofit.http.client;

import com.squareup.okhttp.OkHttpClient;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

/** Retrofit client that uses OkHttp for communication. */
public class OkClient extends UrlConnectionClient {
private final OkHttpClient client;

public OkClient() {
this(new OkHttpClient());
}

public OkClient(OkHttpClient client) {
this.client = client;
}

@Override protected HttpURLConnection openConnection(Request request) throws IOException {
return client.open(new URL(request.getUrl()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import retrofit.http.mime.TypedInput;
import retrofit.http.mime.TypedOutput;

/** Retrofit client that uses {@link HttpURLConnection} for communication. */
public class UrlConnectionClient implements Client {
@Override public Response execute(Request request) throws IOException {
HttpURLConnection connection = openConnection(request);
Expand Down

0 comments on commit bdaf792

Please sign in to comment.