Bläddra i källkod

Upgrade OkHttp to 3.0.1

inorichi 9 år sedan
förälder
incheckning
733b0da461

+ 3 - 2
app/build.gradle

@@ -81,6 +81,7 @@ android {
 dependencies {
     final SUPPORT_LIBRARY_VERSION = '23.1.1'
     final DAGGER_VERSION = '2.0.2'
+    final OKHTTP_VERSION = '3.0.1'
     final MOCKITO_VERSION = '1.10.19'
     final STORIO_VERSION = '1.8.0'
     final ICEPICK_VERSION = '3.1.0'
@@ -95,8 +96,8 @@ dependencies {
     compile "com.android.support:recyclerview-v7:$SUPPORT_LIBRARY_VERSION"
     compile "com.android.support:support-annotations:$SUPPORT_LIBRARY_VERSION"
     compile "com.android.support:percent:$SUPPORT_LIBRARY_VERSION"
-    compile 'com.squareup.okhttp:okhttp-urlconnection:2.7.2'
-    compile 'com.squareup.okhttp:okhttp:2.7.2'
+    compile "com.squareup.okhttp3:okhttp:$OKHTTP_VERSION"
+    compile "com.squareup.okhttp3:okhttp-urlconnection:$OKHTTP_VERSION"
     compile 'com.squareup.okio:okio:1.6.0'
     compile 'com.google.code.gson:gson:2.5'
     compile 'com.jakewharton:disklrucache:2.0.2'

+ 1 - 1
app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.java

@@ -6,7 +6,6 @@ import android.text.format.Formatter;
 import com.google.gson.Gson;
 import com.google.gson.reflect.TypeToken;
 import com.jakewharton.disklrucache.DiskLruCache;
-import com.squareup.okhttp.Response;
 
 import java.io.BufferedOutputStream;
 import java.io.File;
@@ -17,6 +16,7 @@ import java.util.List;
 
 import eu.kanade.tachiyomi.data.source.model.Page;
 import eu.kanade.tachiyomi.util.DiskUtils;
+import okhttp3.Response;
 import okio.BufferedSink;
 import okio.Okio;
 import rx.Observable;

+ 1 - 2
app/src/main/java/eu/kanade/tachiyomi/data/mangasync/base/MangaSyncService.java

@@ -1,8 +1,7 @@
 package eu.kanade.tachiyomi.data.mangasync.base;
 
-import com.squareup.okhttp.Response;
-
 import eu.kanade.tachiyomi.data.database.models.MangaSync;
+import okhttp3.Response;
 import rx.Observable;
 
 public abstract class MangaSyncService {

+ 6 - 7
app/src/main/java/eu/kanade/tachiyomi/data/mangasync/services/MyAnimeList.java

@@ -4,12 +4,6 @@ import android.content.Context;
 import android.net.Uri;
 import android.util.Xml;
 
-import com.squareup.okhttp.Credentials;
-import com.squareup.okhttp.FormEncodingBuilder;
-import com.squareup.okhttp.Headers;
-import com.squareup.okhttp.RequestBody;
-import com.squareup.okhttp.Response;
-
 import org.jsoup.Jsoup;
 import org.xmlpull.v1.XmlSerializer;
 
@@ -26,6 +20,11 @@ import eu.kanade.tachiyomi.data.mangasync.MangaSyncManager;
 import eu.kanade.tachiyomi.data.mangasync.base.MangaSyncService;
 import eu.kanade.tachiyomi.data.network.NetworkHelper;
 import eu.kanade.tachiyomi.data.preference.PreferencesHelper;
+import okhttp3.Credentials;
+import okhttp3.FormBody;
+import okhttp3.Headers;
+import okhttp3.RequestBody;
+import okhttp3.Response;
 import rx.Observable;
 
 public class MyAnimeList extends MangaSyncService {
@@ -209,7 +208,7 @@ public class MyAnimeList extends MangaSyncService {
         xml.endTag("", ENTRY_TAG);
         xml.endDocument();
 
-        FormEncodingBuilder form = new FormEncodingBuilder();
+        FormBody.Builder form = new FormBody.Builder();
         form.add("data", writer.toString());
         return form.build();
     }

+ 33 - 19
app/src/main/java/eu/kanade/tachiyomi/data/network/NetworkHelper.java

@@ -1,34 +1,47 @@
 package eu.kanade.tachiyomi.data.network;
 
 
-import com.squareup.okhttp.CacheControl;
-import com.squareup.okhttp.FormEncodingBuilder;
-import com.squareup.okhttp.Headers;
-import com.squareup.okhttp.OkHttpClient;
-import com.squareup.okhttp.Request;
-import com.squareup.okhttp.RequestBody;
-import com.squareup.okhttp.Response;
+import android.content.Context;
 
+import java.io.File;
 import java.net.CookieManager;
 import java.net.CookiePolicy;
 import java.net.CookieStore;
 
+import okhttp3.Cache;
+import okhttp3.CacheControl;
+import okhttp3.FormBody;
+import okhttp3.Headers;
+import okhttp3.JavaNetCookieJar;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
 import rx.Observable;
 
 public final class NetworkHelper {
 
     private OkHttpClient client;
+
     private CookieManager cookieManager;
 
     public final CacheControl NULL_CACHE_CONTROL = new CacheControl.Builder().noCache().build();
     public final Headers NULL_HEADERS = new Headers.Builder().build();
-    public final RequestBody NULL_REQUEST_BODY = new FormEncodingBuilder().build();
+    public final RequestBody NULL_REQUEST_BODY = new FormBody.Builder().build();
+
+    private static final int CACHE_SIZE = 5 * 1024 * 1024; // 5 MiB
+    private static final String CACHE_DIR_NAME = "network_cache";
+
+    public NetworkHelper(Context context) {
+        File cacheDir = new File(context.getCacheDir(), CACHE_DIR_NAME);
 
-    public NetworkHelper() {
-        client = new OkHttpClient();
         cookieManager = new CookieManager();
         cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
-        client.setCookieHandler(cookieManager);
+
+        client = new OkHttpClient.Builder()
+                .cookieJar(new JavaNetCookieJar(cookieManager))
+                .cache(new Cache(cacheDir, CACHE_SIZE))
+                .build();
     }
 
     public Observable<Response> getResponse(final String url, final Headers headers, final CacheControl cacheControl) {
@@ -86,19 +99,20 @@ public final class NetworkHelper {
                         .headers(headers != null ? headers : NULL_HEADERS)
                         .build();
 
-                OkHttpClient progressClient = client.clone();
+                OkHttpClient progressClient = client.newBuilder()
+                        .cache(null)
+                        .addNetworkInterceptor(chain -> {
+                            Response originalResponse = chain.proceed(chain.request());
+                            return originalResponse.newBuilder()
+                                    .body(new ProgressResponseBody(originalResponse.body(), listener))
+                                    .build();
+                        }).build();
 
-                progressClient.networkInterceptors().add(chain -> {
-                    Response originalResponse = chain.proceed(chain.request());
-                    return originalResponse.newBuilder()
-                            .body(new ProgressResponseBody(originalResponse.body(), listener))
-                            .build();
-                });
                 return Observable.just(progressClient.newCall(request).execute());
             } catch (Throwable e) {
                 return Observable.error(e);
             }
-        }).retry(2);
+        }).retry(1);
     }
 
     public CookieStore getCookies() {

+ 5 - 5
app/src/main/java/eu/kanade/tachiyomi/data/network/ProgressResponseBody.java

@@ -1,10 +1,9 @@
 package eu.kanade.tachiyomi.data.network;
 
-import com.squareup.okhttp.MediaType;
-import com.squareup.okhttp.ResponseBody;
-
 import java.io.IOException;
 
+import okhttp3.MediaType;
+import okhttp3.ResponseBody;
 import okio.Buffer;
 import okio.BufferedSource;
 import okio.ForwardingSource;
@@ -26,11 +25,11 @@ public class ProgressResponseBody extends ResponseBody {
         return responseBody.contentType();
     }
 
-    @Override public long contentLength() throws IOException {
+    @Override public long contentLength() {
         return responseBody.contentLength();
     }
 
-    @Override public BufferedSource source() throws IOException {
+    @Override public BufferedSource source() {
         if (bufferedSource == null) {
             bufferedSource = Okio.buffer(source(responseBody.source()));
         }
@@ -40,6 +39,7 @@ public class ProgressResponseBody extends ResponseBody {
     private Source source(Source source) {
         return new ForwardingSource(source) {
             long totalBytesRead = 0L;
+
             @Override public long read(Buffer sink, long byteCount) throws IOException {
                 long bytesRead = super.read(sink, byteCount);
                 // read() returns the number of bytes read, or -1 if this source is exhausted.

+ 2 - 3
app/src/main/java/eu/kanade/tachiyomi/data/source/base/BaseSource.java

@@ -1,8 +1,5 @@
 package eu.kanade.tachiyomi.data.source.base;
 
-import com.squareup.okhttp.Headers;
-import com.squareup.okhttp.Response;
-
 import org.jsoup.nodes.Document;
 
 import java.util.List;
@@ -10,6 +7,8 @@ import java.util.List;
 import eu.kanade.tachiyomi.data.database.models.Chapter;
 import eu.kanade.tachiyomi.data.database.models.Manga;
 import eu.kanade.tachiyomi.data.source.model.MangasPage;
+import okhttp3.Headers;
+import okhttp3.Response;
 import rx.Observable;
 
 public abstract class BaseSource {

+ 2 - 2
app/src/main/java/eu/kanade/tachiyomi/data/source/base/Source.java

@@ -3,8 +3,6 @@ package eu.kanade.tachiyomi.data.source.base;
 import android.content.Context;
 
 import com.bumptech.glide.load.model.LazyHeaders;
-import com.squareup.okhttp.Headers;
-import com.squareup.okhttp.Response;
 
 import org.jsoup.Jsoup;
 
@@ -23,6 +21,8 @@ import eu.kanade.tachiyomi.data.network.NetworkHelper;
 import eu.kanade.tachiyomi.data.preference.PreferencesHelper;
 import eu.kanade.tachiyomi.data.source.model.MangasPage;
 import eu.kanade.tachiyomi.data.source.model.Page;
+import okhttp3.Headers;
+import okhttp3.Response;
 import rx.Observable;
 import rx.schedulers.Schedulers;
 

+ 11 - 7
app/src/main/java/eu/kanade/tachiyomi/data/source/online/english/Batoto.java

@@ -4,10 +4,6 @@ import android.content.Context;
 import android.net.Uri;
 import android.text.TextUtils;
 
-import com.squareup.okhttp.FormEncodingBuilder;
-import com.squareup.okhttp.Headers;
-import com.squareup.okhttp.Response;
-
 import org.jsoup.Jsoup;
 import org.jsoup.nodes.Document;
 import org.jsoup.nodes.Element;
@@ -35,6 +31,9 @@ import eu.kanade.tachiyomi.data.source.base.LoginSource;
 import eu.kanade.tachiyomi.data.source.model.MangasPage;
 import eu.kanade.tachiyomi.data.source.model.Page;
 import eu.kanade.tachiyomi.util.Parser;
+import okhttp3.FormBody;
+import okhttp3.Headers;
+import okhttp3.Response;
 import rx.Observable;
 
 public class Batoto extends LoginSource {
@@ -320,7 +319,7 @@ public class Batoto extends LoginSource {
         Element form = doc.select("#login").first();
         String postUrl = form.attr("action");
 
-        FormEncodingBuilder formBody = new FormEncodingBuilder();
+        FormBody.Builder formBody = new FormBody.Builder();
         Element authKey = form.select("input[name=auth_key]").first();
 
         formBody.add(authKey.attr("name"), authKey.attr("value"));
@@ -354,8 +353,13 @@ public class Batoto extends LoginSource {
     @Override
     public Observable<List<Chapter>> pullChaptersFromNetwork(String mangaUrl) {
         Observable<List<Chapter>> observable;
-        if (!isLogged()) {
-            observable = login(prefs.getSourceUsername(this), prefs.getSourcePassword(this))
+        String username = prefs.getSourceUsername(this);
+        String password = prefs.getSourcePassword(this);
+        if (username.isEmpty() && password.isEmpty()) {
+            observable = Observable.error(new Exception("User not logged"));
+        }
+        else if (!isLogged()) {
+            observable = login(username, password)
                     .flatMap(result -> super.pullChaptersFromNetwork(mangaUrl));
         }
         else {

+ 4 - 5
app/src/main/java/eu/kanade/tachiyomi/data/source/online/english/Kissmanga.java

@@ -3,10 +3,6 @@ package eu.kanade.tachiyomi.data.source.online.english;
 import android.content.Context;
 import android.net.Uri;
 
-import com.squareup.okhttp.FormEncodingBuilder;
-import com.squareup.okhttp.Headers;
-import com.squareup.okhttp.Response;
-
 import org.jsoup.Jsoup;
 import org.jsoup.nodes.Document;
 import org.jsoup.nodes.Element;
@@ -26,6 +22,9 @@ import eu.kanade.tachiyomi.data.source.base.Source;
 import eu.kanade.tachiyomi.data.source.model.MangasPage;
 import eu.kanade.tachiyomi.data.source.model.Page;
 import eu.kanade.tachiyomi.util.Parser;
+import okhttp3.FormBody;
+import okhttp3.Headers;
+import okhttp3.Response;
 import rx.Observable;
 
 public class Kissmanga extends Source {
@@ -109,7 +108,7 @@ public class Kissmanga extends Source {
         if (page.page == 1)
             page.url = getInitialSearchUrl(query);
 
-        FormEncodingBuilder form = new FormEncodingBuilder();
+        FormBody.Builder form = new FormBody.Builder();
         form.add("authorArtist", "");
         form.add("mangaName", query);
         form.add("status", "");

+ 2 - 2
app/src/main/java/eu/kanade/tachiyomi/injection/module/DataModule.java

@@ -47,8 +47,8 @@ public class DataModule {
 
     @Provides
     @Singleton
-    NetworkHelper provideNetworkHelper() {
-        return new NetworkHelper();
+    NetworkHelper provideNetworkHelper(Application app) {
+        return new NetworkHelper(app);
     }
 
     @Provides