Use HTTP/1.1 when new fancy protocols fail

This commit is contained in:
Martin Fietz 2015-10-16 13:44:02 +02:00
parent 0e2b056d4e
commit 75b2dc39c6
1 changed files with 16 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package de.danoeh.antennapod.core.service.download;
import android.util.Log; import android.util.Log;
import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Protocol;
import com.squareup.okhttp.Request; import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response; import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody; import com.squareup.okhttp.ResponseBody;
@ -22,6 +23,7 @@ import java.net.HttpURLConnection;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
import java.net.URI; import java.net.URI;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import de.danoeh.antennapod.core.ClientConfig; import de.danoeh.antennapod.core.ClientConfig;
@ -93,12 +95,23 @@ public class HttpDownloader extends Downloader {
// add range header if necessary // add range header if necessary
if (fileExists) { if (fileExists) {
request.setSoFar(destination.length()); request.setSoFar(destination.length());
httpReq.addHeader("Range", httpReq.addHeader("Range", "bytes=" + request.getSoFar() + "-");
"bytes=" + request.getSoFar() + "-");
Log.d(TAG, "Adding range header: " + request.getSoFar()); Log.d(TAG, "Adding range header: " + request.getSoFar());
} }
Response response = httpClient.newCall(httpReq.build()).execute(); Response response = null;
try {
response = httpClient.newCall(httpReq.build()).execute();
} catch(IOException e) {
Log.e(TAG, e.toString());
if(e.getMessage().contains("PROTOCOL_ERROR")) {
httpClient.setProtocols(Arrays.asList(Protocol.HTTP_1_1));
response = httpClient.newCall(httpReq.build()).execute();
}
else {
throw e;
}
}
responseBody = response.body(); responseBody = response.body();
String contentEncodingHeader = response.header("Content-Encoding"); String contentEncodingHeader = response.header("Content-Encoding");
boolean isGzip = StringUtils.equalsIgnoreCase(contentEncodingHeader, "gzip"); boolean isGzip = StringUtils.equalsIgnoreCase(contentEncodingHeader, "gzip");