我似乎收到了损坏的文件,我需要知道这是否有可能,并确保我处理得当。
我有一个可以下载大量背靠背音乐文件的应用程序。如果使用以下方法下载时,连接中突然失去连接会发生什么?我会发现异常吗?还是我会返回一半完成(损坏)的数据?我需要确保我没有试图保存不完整的数据。
public static InputStream getHTTPRequest(HttpGet getRequest, HttpParams params)
{
final String TAG = "getHTTPRequest";
if (params != null)
{
getRequest.setParams(params);
}
DefaultHttpClient client = new DefaultHttpClient();
if(getRequest == null)
{
Log.d(TAG, "The HttpGet param passed in was null, returning null");
return null;
}
try {
HttpResponse getResponse = client.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.d(TAG, "status not ok");
Log.d(TAG, "status = " + Integer.toString(statusCode));
Log.d(TAG, "url = " + getRequest.getURI().toString());
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
return getResponseEntity.getContent();
} catch (IOException e) {
Log.d(TAG, "IOException: getRequest.abort");
getRequest.abort();
}
return null;
}