Skip to content

Instantly share code, notes, and snippets.

@stonecs
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stonecs/9068920 to your computer and use it in GitHub Desktop.
Save stonecs/9068920 to your computer and use it in GitHub Desktop.
public class BasicAuthOkHttpLoader extends OkHttpDownloader {
private static final int MAX_SIZE = 1024 * 1024 * 7; // 7MB
private static final int MAX_STALE = 30 * 24 * 60 * 60; // 30 days
@Inject
public BasicAuthOkHttpLoader() {
super(Utils.getImageCacheDirectory(), MAX_SIZE);
}
@Override
protected HttpURLConnection openConnection(Uri uri) throws IOException {
HttpURLConnection connection = super.openConnection(uri);
String authString = getHttpUsername() + ":" + getHttpPassword();
final String authStringEnc = Base64.encodeToString(authString.getBytes(), Base64.NO_WRAP);
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
connection.setRequestProperty("Cache-Control", "max-stale=" + MAX_STALE);
return connection;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment