Skip to content

Instantly share code, notes, and snippets.

@JakeWharton
Created June 17, 2013 15:04
Show Gist options
  • Select an option

  • Save JakeWharton/5797571 to your computer and use it in GitHub Desktop.

Select an option

Save JakeWharton/5797571 to your computer and use it in GitHub Desktop.

Revisions

  1. JakeWharton created this gist Jun 17, 2013.
    29 changes: 29 additions & 0 deletions OkConnectionFactory.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    /**
    * A {@link HttpRequest.ConnectionFactory connection factory} which uses OkHttp.
    * <p/>
    * Call {@link HttpRequest#setConnectionFactory(HttpRequest.ConnectionFactory)} with an instance of
    * this class to enable.
    */
    public class OkConnectionFactory implements HttpRequest.ConnectionFactory {
    private final OkHttpClient client;

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

    public OkConnectionFactory(OkHttpClient client) {
    if (client == null) {
    throw new NullPointerException("Client must not be null.");
    }
    this.client = client;
    }

    public HttpURLConnection create(URL url) throws IOException {
    return client.open(url);
    }

    public HttpURLConnection create(URL url, Proxy proxy) throws IOException {
    throw new UnsupportedOperationException(
    "Per-connection proxy is not supported. Use OkHttpClient's setProxy instead.");
    }
    }