안드로이드 개발 질문/답변
(글 수 45,052)
안녕하세요.
기초 없이 개발하려니 개발괴발이네요 -_-;
네트워크 지식이 없어 애를 먹고 있습니다.
URLConnection을 열고 파일을 다운로드 받는 중에 3G<>WiFi 전환을 하게 되면
무한대기를 타는 문제가 생기네요.
url = new URL("BDK.DB_URL);
conn = url.openConnection();
lastModified = conn.getLastModified();
conn.setReadTimeout(10000);
...
is = conn.getInputStream();
File target = new File(BDK.getDbAbsolutePath(SplashActivity.this));
if (target.exists()) {
target.delete();
}
target.createNewFile();
fos = new FileOutputStream(target);
bos = new BufferedOutputStream(fos);
int bufferLength = 0;
int totalLength = 0;
incrementValue = 0;
byte[] buffer = new byte[1024];
while((bufferLength = is.read(buffer)) > 0) { // 전환시 is.read()에서 무한대기를 타는 듯 보입니다.
bos.write(buffer, 0, bufferLength);
incrementValue = bufferLength;
totalLength += incrementValue;
runOnUiThread(new Runnable() {
@Override
public void run() {
dbDialog.incrementProgressBy(incrementValue);
}
});
Thread.sleep(1);
}
URLConnection에 timeout을 설정해도 전혀 먹질 않더군요.
스레드가 계속 돌도 있으면 변수를 바꾸는 등 처리를 해줄 수 있겠는데
스레드가 is.read()에서 무한대기를 타는 듯 하여 해결법을 찾지 못하고 전전긍긍하고 있습니다 ㅠ
제게 가르침을 주십시오!
lifecluee@gmail.com




음 전 이거 두개쓰는데..
BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
FileOutputStream fos = new FileOutputStream(localFilePath);