diff --git a/app/src/main/java/org/schabi/newpipe/DownloaderImpl.java b/app/src/main/java/org/schabi/newpipe/DownloaderImpl.java index 50972fb2f..98b9a524b 100644 --- a/app/src/main/java/org/schabi/newpipe/DownloaderImpl.java +++ b/app/src/main/java/org/schabi/newpipe/DownloaderImpl.java @@ -52,6 +52,7 @@ public final class DownloaderImpl extends Downloader { private static DownloaderImpl instance; private final Map mCookies; private final OkHttpClient client; + private Integer customTimeout; private DownloaderImpl(final OkHttpClient.Builder builder) { if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) { @@ -130,6 +131,11 @@ public final class DownloaderImpl extends Downloader { } } + public DownloaderImpl setCustomTimeout(final Integer value) { + this.customTimeout = value; + return this; + } + public String getCookies(final String url) { final List resultCookies = new ArrayList<>(); if (url.contains(YOUTUBE_DOMAIN)) { @@ -205,7 +211,18 @@ public final class DownloaderImpl extends Downloader { } final okhttp3.Request request = requestBuilder.build(); - final okhttp3.Response response = client.newCall(request).execute(); + + OkHttpClient tmpClient = client; + final okhttp3.Response response; + + if (customTimeout != null) { + tmpClient = new OkHttpClient.Builder() + .readTimeout(customTimeout, TimeUnit.SECONDS) + .build(); + } + + response = tmpClient.newCall(request).execute(); + final ResponseBody body = response.body(); if (response.code() == 429) { @@ -260,7 +277,16 @@ public final class DownloaderImpl extends Downloader { } - final okhttp3.Response response = client.newCall(requestBuilder.build()).execute(); + OkHttpClient tmpClient = client; + final okhttp3.Response response; + + if (customTimeout != null) { + tmpClient = new OkHttpClient.Builder() + .readTimeout(customTimeout, TimeUnit.SECONDS) + .build(); + } + + response = tmpClient.newCall(requestBuilder.build()).execute(); if (response.code() == 429) { response.close(); diff --git a/app/src/main/java/org/schabi/newpipe/util/SponsorBlockUtils.java b/app/src/main/java/org/schabi/newpipe/util/SponsorBlockUtils.java index 530f1e78a..c87da1b1c 100644 --- a/app/src/main/java/org/schabi/newpipe/util/SponsorBlockUtils.java +++ b/app/src/main/java/org/schabi/newpipe/util/SponsorBlockUtils.java @@ -115,6 +115,7 @@ public final class SponsorBlockUtils { final String responseBody = DownloaderImpl .getInstance() + .setCustomTimeout(3) .get(apiUrl + params) .responseBody();