비슷한 문제가 발생했습니다. HttpUrlConnection won't time out 중간 다운로드가 있기 때문입니다. 예를 들어, 다운로드가 진행되는 동안 wifi를 끄면 광산에서 다운로드가 계속 진행되고 동일한 비율로 고정됩니다.
DownloaderTask라는 AsyncTask에 연결된 TimerTask를 사용하여 솔루션을 찾았습니다. 시도 : 그런 다음 실제 다운로드 루프에서 타임 아웃 오류에 대한 타이머 설정
class Timeout extends TimerTask {
private DownloaderTask _task;
public Timeout(DownloaderTask task) {
_task = task;
}
@Override
public void run() {
Log.w(TAG,"Timed out while downloading.");
_task.cancel(false);
}
};
:
_outFile.createNewFile();
FileOutputStream file = new FileOutputStream(_outFile);
out = new BufferedOutputStream(file);
byte[] data = new byte[1024];
int count;
_timer = new Timer();
// Read in chunks, much more efficient than byte by byte, lower cpu usage.
while((count = in.read(data, 0, 1024)) != -1 && !isCancelled()) {
out.write(data,0,count);
downloaded+=count;
publishProgress((int) ((downloaded/ (float)contentLength)*100));
_timer.cancel();
_timer = new Timer();
_timer.schedule(new Timeout(this), 1000*20);
}
_timer.cancel();
out.flush();
그것은 만약 타임 아웃을 20 초에도 1K 다운로드하지 않습니다, 그것은 대신 취소 영원히 다운로드하는 것처럼 보입니다.
두 가지에 대한 IP를 확인할 수 있습니다. 앱이 멈추지 않게하려면 연결 방법을 별도의 스레드에 넣으십시오. 둘째, 완벽하게 작동한다고 말했습니까? 연결 상태를 잘못 시뮬레이트하기 위해 무엇을하고 있습니까? – Otra
@Otra 나는 progressdialog를 사용하여 별도의 스레드에있다. 기본적으로 발생하는 것은 연결이 양호하면 작업이 그 일을하는 것입니다. 그러나 연결이 나쁜 경우 진행 대화 상자가 오래 유지됩니다. 나쁜 연결을 시뮬레이트하기 위해 타임 아웃 시간을 줄입니다. 15 초를주는 대신 1 초.테스트 용입니다. 아니면 틀린가? –
HttpURLConnection.setReadTimeout (밀리 초); –