У меня в настоящее время есть asynctask
, который загружает mp3 в папку на SDCARD.Большую часть времени она работает нормально, но иногда загрузка просто пропускается, и песня загружается не полностью.Любые идеи, почему это происходит?
Код:
@Override
protected String doInBackground(String... url) {
int count;
try {
URL url2 = new URL(sdrUrl);
HttpURLConnection connection = (HttpURLConnection) url2.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();
int lengthOfFile = connection.getContentLength();
//make the stop drop rave folder
File sdrFolder = new File(Environment.getExternalStorageDirectory() + "/StopDropRave");
boolean success = false;
if(!sdrFolder.exists()){
success = sdrFolder.mkdir();
}
if (!success) {
String PATH = Environment.getExternalStorageDirectory()
+ "/StopDropRave/";
file = new File(PATH);
file.mkdirs();
}
else
{
String PATH = Environment.getExternalStorageDirectory()
+ "/StopDropRave/";
file = new File(PATH);
file.mkdirs();
}
String[] path = url2.getPath().split("/");
String mp3 = path[path.length - 1];
String mp31 = mp3.replace("%20", " ");
String sdrMp3 = mp31.replace("%28", "(");
String sdrMp31 = sdrMp3.replace("%29", ")");
String sdrMp32 = sdrMp31.replace("%27", "'");
File outputFile = new File(file, sdrMp32);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream input = connection.getInputStream();
byte[] data = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int) (total * 100 / lengthOfFile));
fos.write(data, 0, count);
}
fos.close();
input.close();
} catch (IOException e) {
}
return null;
}
Вот что появляется в моей машине журнала:
08-02 19:11:18.834: WARN/System.err(16225): java.net.SocketException: Connection timed out
08-02 19:11:18.843: DEBUG/AK8973(121): HRET = AKSC_HOF_DSS_REQUEST
08-02 19:11:18.853: DEBUG/AK8973(121): HRET = AKSC_HOF_DSS_REQUEST
08-02 19:11:18.873: DEBUG/AK8973(121): HRET = AKSC_HOF_DSS_COMPLETE
08-02 19:11:18.923: WARN/System.err(16225): at org.apache.harmony.luni.platform.OSNetworkSystem.read(Native Method)
08-02 19:11:18.983: WARN/System.err(16225): at dalvik.system.BlockGuard$WrappedNetworkSystem.read(BlockGuard.java:273)
08-02 19:11:18.983: WARN/System.err(16225): at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:458)
08-02 19:11:18.983: WARN/System.err(16225): at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:85)
08-02 19:11:18.983: WARN/System.err(16225): at java.io.BufferedInputStream.read(BufferedInputStream.java:319)
08-02 19:11:18.983: WARN/System.err(16225): at org.apache.harmony.luni.internal.net.www.protocol.http.FixedLengthInputStream.read(FixedLengthInputStream.java:44)
08-02 19:11:18.983: WARN/System.err(16225): at java.io.InputStream.read(InputStream.java:157)
08-02 19:11:18.983: WARN/System.err(16225): at ravebox.dev.sdr.BlogActivity$DownloadFile.doInBackground(BlogActivity.java:308)
08-02 19:11:18.993: WARN/System.err(16225): at ravebox.dev.sdr.BlogActivity$DownloadFile.doInBackground(BlogActivity.java:1)
08-02 19:11:18.993: WARN/System.err(16225): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-02 19:11:18.993: WARN/System.err(16225): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-02 19:11:18.993: WARN/System.err(16225): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-02 19:11:18.993: WARN/System.err(16225): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-02 19:11:18.993: WARN/System.err(16225): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-02 19:11:18.993: WARN/System.err(16225): at java.lang.Thread.run(Thread.java:1019)