Skip to content

Commit

Permalink
Everyone is using the same OkHttpClient
Browse files Browse the repository at this point in the history
and happy.
  • Loading branch information
tasomaniac committed Jul 13, 2015
1 parent ac5be26 commit 9e39e9e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
15 changes: 15 additions & 0 deletions app/src/main/java/com/tasomaniac/muzei/comiccovers/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import android.util.Log;

import com.crashlytics.android.Crashlytics;
import com.squareup.okhttp.OkHttpClient;
import com.tasomaniac.muzei.comiccovers.util.IOUtil;

import java.util.concurrent.TimeUnit;

import io.fabric.sdk.android.Fabric;
import timber.log.Timber;
Expand All @@ -14,17 +18,28 @@
*/
public class App extends Application {

private OkHttpClient okHttpClient;

@Override
public void onCreate() {
super.onCreate();

okHttpClient = new OkHttpClient();
okHttpClient.setConnectTimeout(IOUtil.DEFAULT_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS);
okHttpClient.setReadTimeout(IOUtil.DEFAULT_READ_TIMEOUT, TimeUnit.MILLISECONDS);

if (BuildConfig.DEBUG) {
Timber.plant(new Timber.DebugTree());
} else {
Fabric.with(this, new Crashlytics());
Timber.plant(new CrashReportingTree());
}
}

public OkHttpClient getOkHttpClient() {
return okHttpClient;
}

public static App get(Context context) {
return (App) context.getApplicationContext();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import retrofit.RequestInterceptor;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.OkClient;
import timber.log.Timber;

public class ComicVineArtSource extends RemoteMuzeiArtSource {
Expand Down Expand Up @@ -48,6 +49,7 @@ public void onCreate() {
protected void onTryUpdate(int reason) throws RetryException {

RestAdapter restAdapter = new RestAdapter.Builder()
.setClient(new OkClient(App.get(getApplicationContext()).getOkHttpClient()))
.setEndpoint(Config.API_ENDPOINT)
.setRequestInterceptor(new RequestInterceptor() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.tasomaniac.muzei.comiccovers.util;

import android.content.Context;
import android.net.Uri;

import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.OkUrlFactory;
import com.tasomaniac.muzei.comiccovers.App;

import java.io.IOException;
import java.net.HttpURLConnection;
Expand All @@ -13,10 +14,10 @@
import timber.log.Timber;

public class IOUtil {
private static final int DEFAULT_READ_TIMEOUT = 30 * 1000; // 30s
private static final int DEFAULT_CONNECT_TIMEOUT = 15 * 1000; // 15s
public static final int DEFAULT_READ_TIMEOUT = 30 * 1000; // 30s
public static final int DEFAULT_CONNECT_TIMEOUT = 15 * 1000; // 15s

public static boolean checkContentType(Uri uri, String reqContentTypeSubstring)
public static boolean checkContentType(Context context, Uri uri, String reqContentTypeSubstring)
throws OpenUriException {

if (uri == null) {
Expand All @@ -28,12 +29,12 @@ public static boolean checkContentType(Uri uri, String reqContentTypeSubstring)
throw new OpenUriException(false, new IOException("Uri had no scheme"));
}

OkHttpClient client = new OkHttpClient();
HttpURLConnection conn;
int responseCode = 0;
String responseMessage = null;
try {
conn = new OkUrlFactory(client).open(new URL(uri.toString()));
conn = new OkUrlFactory(App.get(context).getOkHttpClient())
.open(new URL(uri.toString()));
} catch (MalformedURLException e) {
throw new OpenUriException(false, e);
}
Expand Down

0 comments on commit 9e39e9e

Please sign in to comment.