Skip to content

Commit

Permalink
If OkHttp is present, require 1.6.0 or better.
Browse files Browse the repository at this point in the history
  • Loading branch information
squarejesse committed May 28, 2014
1 parent e94af45 commit b7e9f27
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<android.version>4.1.1.4</android.version>
<android.platform>16</android.platform>
<gson.version>2.2.4</gson.version>
<okhttp.version>1.3.0</okhttp.version>
<okhttp.version>1.6.0</okhttp.version>
<rxjava.version>0.18.3</rxjava.version>
<appengine.version>1.8.9</appengine.version>

Expand Down Expand Up @@ -103,6 +103,11 @@
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp-urlconnection</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 @@ -29,6 +29,11 @@
<artifactId>okhttp</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp-urlconnection</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.netflix.rxjava</groupId>
<artifactId>rxjava-core</artifactId>
Expand Down
24 changes: 21 additions & 3 deletions retrofit/src/main/java/retrofit/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,32 @@ private static class AppEngine extends Base {
}
}

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

boolean okHttpClient = false;
try {
Class.forName("com.squareup.okhttp.OkHttpClient");
return true;
okHttpClient = true;
} catch (ClassNotFoundException e) {
return false;
}

if (okHttpClient != okUrlFactory) {
throw new RuntimeException(""
+ "Retrofit detected an unsupported OkHttp on the classpath.\n"
+ "To use OkHttp with this version of Retrofit, you'll need:\n"
+ "1. com.squareup.okhttp:okhttp:1.6.0 (or newer)\n"
+ "2. com.squareup.okhttp:okhttp-urlconnection:1.6.0 (or newer)\n"
+ "Note that OkHttp 2.0.0+ is supported!");
}

return okHttpClient;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions retrofit/src/main/java/retrofit/client/OkClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package retrofit.client;

import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.OkUrlFactory;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
Expand All @@ -30,17 +31,17 @@ private static OkHttpClient generateDefaultOkHttp() {
return client;
}

private final OkHttpClient client;
private final OkUrlFactory okUrlFactory;

public OkClient() {
this(generateDefaultOkHttp());
}

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

@Override protected HttpURLConnection openConnection(Request request) throws IOException {
return client.open(new URL(request.getUrl()));
return okUrlFactory.open(new URL(request.getUrl()));
}
}

0 comments on commit b7e9f27

Please sign in to comment.