انجمن سایت کلیدستان


رتبه موضوع:
  • 1 رای - 5 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
نحوه حذف فایلی که در اثر قطع شدن اینترنت ، ناقص دانلود شده !

نحوه حذف فایلی که در اثر قطع شدن اینترنت ، ناقص دانلود شده !

#6
باید ببینید که کدام Exception مربوط به قطع اینترنت می باشد. یعنی ما می خواهیم که به محض قطع شدن اینترنت، تشخیص بدهیم و کد مربوط به حذف فایل را اجرا کنیم. مثلا من بخشی از کد نوشته شده در کلید زیر را در اینجا براتون می نویسم :

http://www.kelidestan.com/keys/keys.php?key=296

بخش اصلی مربوط به دانلود، به صورت زیر می باشد :

کد:
try {
                     InputStream input = null;
                     OutputStream output = null;
                     HttpURLConnection connection = null;
                     try {
                         URL url = new URL(sUrl[0]);
                         connection = (HttpURLConnection) url.openConnection();
                         connection.connect();

                         // expect HTTP 200 OK, so we don't mistakenly save error report
                         // instead of the file
                         if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
                              return "Server returned HTTP " + connection.getResponseCode()
                                  + " " + connection.getResponseMessage();

                         // this will be useful to display download percentage
                         // might be -1: server did not report the length
                         int fileLength = connection.getContentLength();

                         // download the file
                         input = connection.getInputStream();
                         output = new FileOutputStream("/sdcard/myFile.apk");  //   /sdcard/file_name.extension

                         byte data[] = new byte[4096];
                         long total = 0;
                         int count;
                         while ((count = input.read(data)) != -1) {
                             // allow canceling with back button
                             if (isCancelled())
                                 return null;
                             total += count;
                             // publishing the progress....
                             if (fileLength > 0) // only if total length is known
                                 publishProgress((int) (total * 100 / fileLength));
                             output.write(data, 0, count);
                         }
                     } catch (Exception e) {
                         return e.toString();
                     } finally {
                         try {
                             if (output != null)
                                 output.close();
                             if (input != null)
                                 input.close();
                         }
                         catch (IOException ignored) { }

                         if (connection != null)
                             connection.disconnect();
                     }
                 } finally {
                     wl.release();
                 }

خوب حالا شما باید ببینید که قطع اینترنت، کدام Exception را به وجود می آورد و در کدهای مربوط به آن، کد حذف فایل را بنویسید (حذف در صورتی که قبلا ساخته شده باشد).
برای try و catch و finally ، بد نیست نگاهی به راهنمای زیر بیندازید :

کد:
try {
    // Normally this code runs from the top of the block to the bottom
    // without problems. But it can sometimes throw and exception,
    // either directly or with a throw statement or intdirectly by calling
    // a method that throws and exception.
}
catch (SomeException e1) {
    // This block contains statements that handle the exception object
    // of type SomeException or a subclass of that type. Statements in
    // this block can refer to that exception object by the name e1.
}
catch (AnotherException e2) {
    // This block contains statements that handle the exception object
    // of type AnotherException or a subclass of that type. Statements in
    // this block can refer to that exception object by the name e2.
}
finally {
    // This block contains statements that are ALWAYS executed
    // after leaving the try clause, regardless of whether we leave it:
    // 1) normally after reaching the bottom of the block;
    // 2) because of a break, continue, or return statement;
    // 3) with an exception handled by a catch clause above; or
    // 4) with an uncaught exception that has not been handled.
    // If the try clause calls System.exit(), however, the interpreter
    // exits before the finally clause can be run.
}

bookbook 
لطفا برای درج کد، از دکمه مخصوص درج کد در ادیتور انجمن استفاده کنید.
در مورد برنامه نویسی، مدیران تنها راهنمایی می کنند و نوشتن برنامه نهایی، به عهده کاربران می باشد (اینجا محلی برای یادگیری است، نه سفارش کدنویسی).
کاربران باید ابتدا خود به خطایابی برنامه بپردازند، نه اینکه به محض دیدن خطا، کدها را در انجمن، copy و paste کرده و از مدیران انتظار بررسی داشته باشند.
پاسخ


پیام‌های این موضوع
RE: نحوه حذف فایلی که در اثر قطع شدن اینترنت ، ناقص دانلود شده ! - توسط admin - ۱۳۹۳/۰۲/۰۶, ۰۱:۵۶ ب.ظ

پرش به انجمن:


کاربران در حال بازدید این موضوع: 3 مهمان