我有一個Service
,它使用HttpURLConnection
來管理下載任務。如何管理與HttpURLConnection丟失的連接?
在執行任何HTTP請求之前,我使用自定義isNetworkAvailable
方法,但我想知道如何管理請求期間丟失的連接。
我應該在while
循環中調用我的isNetworkAvailable
方法嗎(這意味着很多調用)?管理與HttpURLConnection
丟失連接的標準方式是什麼?
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
FileOutputStream outputStream = openFileOutput(filename, MODE_PRIVATE | MODE_APPEND);
BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
byte[] data = new byte[1024];
int x = 0;
while ((x = in.read(data, 0, 1024)) >= 0) {
outputStream.write(data, 0, x);
}
好吧,我剛剛看到'IOException'有一個'ConnectException'子類。我會用這個,謝謝。關於'BufferedInputStream':我需要小塊來顯示下載進度。 – jul