Upgrade OkHttp to 3.0.1

This commit is contained in:
inorichi 2016-01-28 16:44:18 +01:00
parent db074a371d
commit 733b0da461
11 changed files with 70 additions and 55 deletions

View file

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

View file

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

View file

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

View file

@ -4,12 +4,6 @@ import android.content.Context;
import android.net.Uri; import android.net.Uri;
import android.util.Xml; 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.jsoup.Jsoup;
import org.xmlpull.v1.XmlSerializer; 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.mangasync.base.MangaSyncService;
import eu.kanade.tachiyomi.data.network.NetworkHelper; import eu.kanade.tachiyomi.data.network.NetworkHelper;
import eu.kanade.tachiyomi.data.preference.PreferencesHelper; 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; import rx.Observable;
public class MyAnimeList extends MangaSyncService { public class MyAnimeList extends MangaSyncService {
@ -209,7 +208,7 @@ public class MyAnimeList extends MangaSyncService {
xml.endTag("", ENTRY_TAG); xml.endTag("", ENTRY_TAG);
xml.endDocument(); xml.endDocument();
FormEncodingBuilder form = new FormEncodingBuilder(); FormBody.Builder form = new FormBody.Builder();
form.add("data", writer.toString()); form.add("data", writer.toString());
return form.build(); return form.build();
} }

View file

@ -1,34 +1,47 @@
package eu.kanade.tachiyomi.data.network; package eu.kanade.tachiyomi.data.network;
import com.squareup.okhttp.CacheControl; import android.content.Context;
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 java.io.File;
import java.net.CookieManager; import java.net.CookieManager;
import java.net.CookiePolicy; import java.net.CookiePolicy;
import java.net.CookieStore; 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; import rx.Observable;
public final class NetworkHelper { public final class NetworkHelper {
private OkHttpClient client; private OkHttpClient client;
private CookieManager cookieManager; private CookieManager cookieManager;
public final CacheControl NULL_CACHE_CONTROL = new CacheControl.Builder().noCache().build(); public final CacheControl NULL_CACHE_CONTROL = new CacheControl.Builder().noCache().build();
public final Headers NULL_HEADERS = new Headers.Builder().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 = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); 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) { 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) .headers(headers != null ? headers : NULL_HEADERS)
.build(); .build();
OkHttpClient progressClient = client.clone(); OkHttpClient progressClient = client.newBuilder()
.cache(null)
progressClient.networkInterceptors().add(chain -> { .addNetworkInterceptor(chain -> {
Response originalResponse = chain.proceed(chain.request()); Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder() return originalResponse.newBuilder()
.body(new ProgressResponseBody(originalResponse.body(), listener)) .body(new ProgressResponseBody(originalResponse.body(), listener))
.build(); .build();
}); }).build();
return Observable.just(progressClient.newCall(request).execute()); return Observable.just(progressClient.newCall(request).execute());
} catch (Throwable e) { } catch (Throwable e) {
return Observable.error(e); return Observable.error(e);
} }
}).retry(2); }).retry(1);
} }
public CookieStore getCookies() { public CookieStore getCookies() {

View file

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

View file

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

View file

@ -3,8 +3,6 @@ package eu.kanade.tachiyomi.data.source.base;
import android.content.Context; import android.content.Context;
import com.bumptech.glide.load.model.LazyHeaders; import com.bumptech.glide.load.model.LazyHeaders;
import com.squareup.okhttp.Headers;
import com.squareup.okhttp.Response;
import org.jsoup.Jsoup; 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.preference.PreferencesHelper;
import eu.kanade.tachiyomi.data.source.model.MangasPage; import eu.kanade.tachiyomi.data.source.model.MangasPage;
import eu.kanade.tachiyomi.data.source.model.Page; import eu.kanade.tachiyomi.data.source.model.Page;
import okhttp3.Headers;
import okhttp3.Response;
import rx.Observable; import rx.Observable;
import rx.schedulers.Schedulers; import rx.schedulers.Schedulers;

View file

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

View file

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

View file

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