Skip to content

Commit

Permalink
Merge pull request #1136 from wb9688/okhttp-uil
Browse files Browse the repository at this point in the history
Use OkHttp with UIL
  • Loading branch information
theScrabi authored Feb 23, 2018
2 parents 4c1de83 + a2373b8 commit 3088778
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/src/main/java/org/schabi/newpipe/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ private ImageLoaderConfiguration getImageLoaderConfigurations(final int memoryCa
return new ImageLoaderConfiguration.Builder(this)
.memoryCache(new LRULimitedMemoryCache(memoryCacheSizeMb * 1024 * 1024))
.diskCacheSize(diskCacheSizeMb * 1024 * 1024)
.imageDownloader(new ImageDownloader(getApplicationContext()))
.build();
}

Expand Down
15 changes: 14 additions & 1 deletion app/src/main/java/org/schabi/newpipe/Downloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -98,6 +99,18 @@ public String download(String siteUrl, String language) throws IOException, ReCa
*/
@Override
public String download(String siteUrl, Map<String, String> customProperties) throws IOException, ReCaptchaException {
return getBody(siteUrl, customProperties).string();
}

public InputStream stream(String siteUrl) throws IOException {
try {
return getBody(siteUrl, Collections.emptyMap()).byteStream();
} catch (ReCaptchaException e) {
throw new IOException(e.getMessage(), e.getCause());
}
}

private ResponseBody getBody(String siteUrl, Map<String, String> customProperties) throws IOException, ReCaptchaException {
final Request.Builder requestBuilder = new Request.Builder()
.method("GET", null).url(siteUrl)
.addHeader("User-Agent", USER_AGENT);
Expand All @@ -123,7 +136,7 @@ public String download(String siteUrl, Map<String, String> customProperties) thr
return null;
}

return body.string();
return body;
}

/**
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/java/org/schabi/newpipe/ImageDownloader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.schabi.newpipe;

import android.content.Context;

import com.nostra13.universalimageloader.core.download.BaseImageDownloader;

import org.schabi.newpipe.extractor.NewPipe;

import java.io.IOException;
import java.io.InputStream;

public class ImageDownloader extends BaseImageDownloader {
public ImageDownloader(Context context) {
super(context);
}

public ImageDownloader(Context context, int connectTimeout, int readTimeout) {
super(context, connectTimeout, readTimeout);
}

protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException {
Downloader downloader = (Downloader) NewPipe.getDownloader();
return downloader.stream(imageUri);
}
}

0 comments on commit 3088778

Please sign in to comment.