mirror of
https://github.com/androidx/media.git
synced 2025-05-07 23:50:44 +08:00
Add responseBody field to InvalidResponseCodeException
Issue #6853 Previously the body of an HTTP response was not included in an InvalidResponseCodeException. This change adds a field to store the response body, and updates DefaultHttpDataSource to populate this value.
This commit is contained in:
parent
7a849e11f7
commit
d94a40ac77
@ -306,6 +306,11 @@ public interface HttpDataSource extends DataSource {
|
|||||||
*/
|
*/
|
||||||
public final Map<String, List<String>> headerFields;
|
public final Map<String, List<String>> headerFields;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The response body, if one was provided.
|
||||||
|
*/
|
||||||
|
@Nullable public final byte[] responseBody;
|
||||||
|
|
||||||
/** @deprecated Use {@link #InvalidResponseCodeException(int, String, Map, DataSpec)}. */
|
/** @deprecated Use {@link #InvalidResponseCodeException(int, String, Map, DataSpec)}. */
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public InvalidResponseCodeException(
|
public InvalidResponseCodeException(
|
||||||
@ -313,15 +318,27 @@ public interface HttpDataSource extends DataSource {
|
|||||||
this(responseCode, /* responseMessage= */ null, headerFields, dataSpec);
|
this(responseCode, /* responseMessage= */ null, headerFields, dataSpec);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @deprecated Use {@link #InvalidResponseCodeException(int, String, Map, DataSpec byte[])}. */
|
||||||
|
@Deprecated
|
||||||
public InvalidResponseCodeException(
|
public InvalidResponseCodeException(
|
||||||
int responseCode,
|
int responseCode,
|
||||||
@Nullable String responseMessage,
|
@Nullable String responseMessage,
|
||||||
Map<String, List<String>> headerFields,
|
Map<String, List<String>> headerFields,
|
||||||
DataSpec dataSpec) {
|
DataSpec dataSpec) {
|
||||||
|
this(responseCode, responseMessage, headerFields, dataSpec, /* responseBody= */ null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public InvalidResponseCodeException(
|
||||||
|
int responseCode,
|
||||||
|
@Nullable String responseMessage,
|
||||||
|
Map<String, List<String>> headerFields,
|
||||||
|
DataSpec dataSpec,
|
||||||
|
@Nullable byte[] responseBody) {
|
||||||
super("Response code: " + responseCode, dataSpec, TYPE_OPEN);
|
super("Response code: " + responseCode, dataSpec, TYPE_OPEN);
|
||||||
this.responseCode = responseCode;
|
this.responseCode = responseCode;
|
||||||
this.responseMessage = responseMessage;
|
this.responseMessage = responseMessage;
|
||||||
this.headerFields = headerFields;
|
this.headerFields = headerFields;
|
||||||
|
this.responseBody = responseBody;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -284,9 +284,16 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou
|
|||||||
}
|
}
|
||||||
|
|
||||||
String responseMessage;
|
String responseMessage;
|
||||||
|
byte[] errorResponseBody = null;
|
||||||
try {
|
try {
|
||||||
responseCode = connection.getResponseCode();
|
responseCode = connection.getResponseCode();
|
||||||
responseMessage = connection.getResponseMessage();
|
responseMessage = connection.getResponseMessage();
|
||||||
|
InputStream errorStream = connection.getErrorStream();
|
||||||
|
// Android Studio says errorStream will never be null, but Android docs say otherwise.
|
||||||
|
// Just check to be sure.
|
||||||
|
if (errorStream != null) {
|
||||||
|
errorResponseBody = Util.toByteArray(errorStream);
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
closeConnectionQuietly();
|
closeConnectionQuietly();
|
||||||
throw new HttpDataSourceException("Unable to connect to " + dataSpec.uri.toString(), e,
|
throw new HttpDataSourceException("Unable to connect to " + dataSpec.uri.toString(), e,
|
||||||
@ -298,7 +305,9 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou
|
|||||||
Map<String, List<String>> headers = connection.getHeaderFields();
|
Map<String, List<String>> headers = connection.getHeaderFields();
|
||||||
closeConnectionQuietly();
|
closeConnectionQuietly();
|
||||||
InvalidResponseCodeException exception =
|
InvalidResponseCodeException exception =
|
||||||
new InvalidResponseCodeException(responseCode, responseMessage, headers, dataSpec);
|
new InvalidResponseCodeException(responseCode, responseMessage, headers, dataSpec,
|
||||||
|
errorResponseBody);
|
||||||
|
|
||||||
if (responseCode == 416) {
|
if (responseCode == 416) {
|
||||||
exception.initCause(new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE));
|
exception.initCause(new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user