1
서버에서 데이터를 수신하는 Android 앱을 작성 중입니다. 이론적으로는 인터넷 연결이 불가능하므로 SocketTimeoutException을 잡아서 재 시도 화면이나 다른 오류 메시지를 표시하여이 사례를 잡으려고합니다. 불행히도이 예외는 발생하지 않습니다. 적어도 그것은 catch 절에 뛰어 들지 않습니다. 내가 도대체 뭘 잘못하고있는 겁니까?SocketTimeoutException 기다리고 있지만 throw되지 않았습니까?
public class HttpConnector {
private String urlString;
private int connectionTimeout = 5000; //milliseconds
public HttpConnector(String urlString) {
this.urlString = urlString;
}
public String receiveData() throws PolizeiwarnungException {
URL url = null;
HttpURLConnection urlConnection = null;
StringBuffer b = new StringBuffer();
try {
url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(connectionTimeout);
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); //Here it gets stuck if there is no connection to the server
String str;
while ((str = reader.readLine()) != null) {
b.append(str + "\n");
}
}
catch (SocketTimeoutException e) {
//TODO
e.printStackTrace();
}
catch (IOException e) {
throw new PolizeiwarnungException(e);
}
finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return b.toString();
}
public void sendData(String data) {
//TODO
}
}
감사합니다. – Bevor