From 79ec4faddb3a9563ce7ba9589c142e6f440c3810 Mon Sep 17 00:00:00 2001 From: len Date: Thu, 26 May 2016 15:16:42 +0200 Subject: [PATCH] Always close requests' response body --- .../data/mangasync/services/MyAnimeList.kt | 8 +++---- .../tachiyomi/data/network/NetworkHelper.kt | 24 +++++++++++++------ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/mangasync/services/MyAnimeList.kt b/app/src/main/java/eu/kanade/tachiyomi/data/mangasync/services/MyAnimeList.kt index b4f9e7aa97..bd45b3ca3e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/mangasync/services/MyAnimeList.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/mangasync/services/MyAnimeList.kt @@ -77,8 +77,8 @@ class MyAnimeList(private val context: Context, id: Int) : MangaSyncService(cont } fun search(query: String): Observable> { - return networkService.requestBody(get(getSearchUrl(query), headers)) - .map { Jsoup.parse(it) } + return networkService.request(get(getSearchUrl(query), headers)) + .map { Jsoup.parse(it.body().string()) } .flatMap { Observable.from(it.select("entry")) } .filter { it.select("type").text() != "Novel" } .map { @@ -102,8 +102,8 @@ class MyAnimeList(private val context: Context, id: Int) : MangaSyncService(cont // MAL doesn't support score with decimals fun getList(): Observable> { - return networkService.requestBody(get(getListUrl(username), headers), networkService.forceCacheClient) - .map { Jsoup.parse(it) } + return networkService.request(get(getListUrl(username), headers), networkService.forceCacheClient) + .map { Jsoup.parse(it.body().string()) } .flatMap { Observable.from(it.select("manga")) } .map { val manga = MangaSync.create(this) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/network/NetworkHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/network/NetworkHelper.kt index 1b61f14697..2ffcd32c26 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/network/NetworkHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/network/NetworkHelper.kt @@ -6,7 +6,9 @@ import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.Response import rx.Observable +import rx.subscriptions.Subscriptions import java.io.File +import java.io.IOException class NetworkHelper(context: Context) { @@ -39,14 +41,22 @@ class NetworkHelper(context: Context) { get() = cookieManager.store fun request(request: Request, client: OkHttpClient = defaultClient): Observable { - return Observable.fromCallable { - client.newCall(request).execute() - } - } + return Observable.create { subscriber -> + val call = client.newCall(request) + subscriber.add(Subscriptions.create { call.cancel() }) - fun requestBody(request: Request, client: OkHttpClient = defaultClient): Observable { - return Observable.fromCallable { - client.newCall(request).execute().body().string() + try { + val response = call.execute() + if (!subscriber.isUnsubscribed) { + subscriber.add(Subscriptions.create { response.body().close() }) + subscriber.onNext(response) + subscriber.onCompleted() + } + } catch (error: IOException) { + if (!subscriber.isUnsubscribed) { + subscriber.onError(error) + } + } } }