Use AndroidHttpClient instead of HttpURLConnection

This commit is contained in:
daniel oeh 2012-12-31 21:26:45 +01:00
parent 19297a1c25
commit 4de65bdd4b
2 changed files with 46 additions and 13 deletions

View File

@ -15,8 +15,18 @@ import java.net.UnknownHostException;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.http.HttpConnection; import org.apache.http.HttpConnection;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import android.net.http.AndroidHttpClient;
import android.util.Log; import android.util.Log;
import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.PodcastApp;
@ -33,10 +43,22 @@ public class HttpDownloader extends Downloader {
private static final int BUFFER_SIZE = 8 * 1024; private static final int BUFFER_SIZE = 8 * 1024;
private static final int CONNECTION_TIMEOUT = 5000; private static final int CONNECTION_TIMEOUT = 5000;
public HttpDownloader(DownloaderCallback downloaderCallback, DownloadStatus status) { public HttpDownloader(DownloaderCallback downloaderCallback,
DownloadStatus status) {
super(downloaderCallback, status); super(downloaderCallback, status);
} }
private AndroidHttpClient createHttpClient() {
AndroidHttpClient httpClient = AndroidHttpClient.newInstance("");
HttpParams params = httpClient.getParams();
params.setIntParameter("http.protocol.max-redirects", MAX_REDIRECTS);
params.setBooleanParameter("http.protocol.reject-relative-redirect",
false);
params.setIntParameter("http.socket.timeout", CONNECTION_TIMEOUT);
HttpClientParams.setRedirecting(params, true);
return httpClient;
}
/** /**
* This method is called by establishConnection(String). Don't call it * This method is called by establishConnection(String). Don't call it
* directly. * directly.
@ -96,21 +118,25 @@ public class HttpDownloader extends Downloader {
@Override @Override
protected void download() { protected void download() {
HttpURLConnection connection = null; AndroidHttpClient httpClient = null;
OutputStream out = null; OutputStream out = null;
InputStream connection = null;
try { try {
connection = establishConnection(status.getFeedFile() HttpGet httpGet = new HttpGet(status.getFeedFile()
.getDownload_url()); .getDownload_url());
if (connection != null) { httpClient = createHttpClient();
if (AppConfig.DEBUG) { HttpResponse response = httpClient.execute(httpGet);
Log.d(TAG, "Connected to resource"); HttpEntity httpEntity = response.getEntity();
} int responseCode = response.getStatusLine().getStatusCode();
if (AppConfig.DEBUG)
Log.d(TAG, "Response code is " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK && httpEntity != null) {
if (StorageUtils.storageAvailable(PodcastApp.getInstance())) { if (StorageUtils.storageAvailable(PodcastApp.getInstance())) {
File destination = new File(status.getFeedFile() File destination = new File(status.getFeedFile()
.getFile_url()); .getFile_url());
if (!destination.exists()) { if (!destination.exists()) {
InputStream in = new BufferedInputStream( connection = httpEntity.getContent();
connection.getInputStream()); InputStream in = new BufferedInputStream(connection);
out = new BufferedOutputStream(new FileOutputStream( out = new BufferedOutputStream(new FileOutputStream(
destination)); destination));
byte[] buffer = new byte[BUFFER_SIZE]; byte[] buffer = new byte[BUFFER_SIZE];
@ -118,10 +144,10 @@ public class HttpDownloader extends Downloader {
status.setStatusMsg(R.string.download_running); status.setStatusMsg(R.string.download_running);
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "Getting size of download"); Log.d(TAG, "Getting size of download");
status.setSize(connection.getContentLength()); status.setSize(httpEntity.getContentLength());
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "Size is " + status.getSize()); Log.d(TAG, "Size is " + status.getSize());
if (status.getSize() == -1) { if (status.getSize() < 0) {
status.setSize(DownloadStatus.SIZE_UNKNOWN); status.setSize(DownloadStatus.SIZE_UNKNOWN);
} }
@ -154,8 +180,11 @@ public class HttpDownloader extends Downloader {
} else { } else {
onFail(DownloadError.ERROR_DEVICE_NOT_FOUND, null); onFail(DownloadError.ERROR_DEVICE_NOT_FOUND, null);
} }
} else {
onFail(DownloadError.ERROR_HTTP_DATA_ERROR,
String.valueOf(responseCode));
} }
} catch (MalformedURLException e) { } catch (IllegalArgumentException e) {
e.printStackTrace(); e.printStackTrace();
onFail(DownloadError.ERROR_MALFORMED_URL, e.getMessage()); onFail(DownloadError.ERROR_MALFORMED_URL, e.getMessage());
} catch (SocketTimeoutException e) { } catch (SocketTimeoutException e) {
@ -173,8 +202,11 @@ public class HttpDownloader extends Downloader {
onFail(DownloadError.ERROR_CONNECTION_ERROR, status.getFeedFile() onFail(DownloadError.ERROR_CONNECTION_ERROR, status.getFeedFile()
.getDownload_url()); .getDownload_url());
} finally { } finally {
IOUtils.close(connection); IOUtils.closeQuietly(connection);
IOUtils.closeQuietly(out); IOUtils.closeQuietly(out);
if (httpClient != null) {
httpClient.close();
}
} }
} }

View File

@ -6,6 +6,7 @@ public class TestDownloads {
"http://httpbin.org/relative-redirect/4", "http://httpbin.org/relative-redirect/4",
"http://jigsaw.w3.org/HTTP/300/307.html", "http://jigsaw.w3.org/HTTP/300/307.html",
"http://radiobox.omroep.nl/programme/read_programme_podcast/9168/read.rss", "http://radiobox.omroep.nl/programme/read_programme_podcast/9168/read.rss",
"http://content.zdf.de/podcast/zdf_heute/heute_a.xml",
"http://rss.sciam.com/sciam/60secsciencepodcast", "http://rss.sciam.com/sciam/60secsciencepodcast",
"http://rss.sciam.com/sciam/60-second-mind", "http://rss.sciam.com/sciam/60-second-mind",
"http://rss.sciam.com/sciam/60-second-space", "http://rss.sciam.com/sciam/60-second-space",