HttpsURLConnection과 연결 한 후 입력 버퍼를 읽습니다. 반환 된 데이터가 비어있는 경우 (0 바이트의 경우 200 만) reader.readLine()은 IOException을 발생시킵니다. (편집 : 사실은 타임 아웃입니다.)BufferedReader.readLine() : 데이터가 비어있는 경우 예외를 피하는 방법?
참고 : 버퍼가 모든 것을 수신 할 때까지 기다릴 필요가있을 수 있으므로 IF는 "우리는 모든 것을 받았지만 아무 것도 없습니다."
감사합니다.
HttpsURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL("https://example.com/GiveMeSomTextPlain");
connection = (HttpsURLConnection) url.openConnection();
connection.setConnectTimeout(9000);
connection.setReadTimeout(11000);
connection.connect();
int resp = connection.getResponseCode();
if (resp == 200) {
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
// if(reader.ThereWillBeNoData()){ // THIS IS WHAT I NEED
// ...
// }
String line = "";
while((line = reader.readLine()) != null){ // EXCEPTION IF EMPTY DATA
// ...
}
}else{
reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
}
} catch (IOException e) {
e.printStackTrace();
// WE GET HERE AT EXCEPTION
} finally {
if (connection != null) connection.disconnect();
try {
if (reader != null) reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
편집 : 아, 그것은 "빈"하지만합니다 (I 입력 (11S)에 해당) 답변이 너무 제한 시간에 끝나는, 그래서 어떻게 시간 초과를 피하기 위해 보인다? (MY 응답)
스택 추적 : 문제의
09-13 18:39:40.134 20925-21658/com.theapp D/ZZZ: BEFORE WHILE
09-13 18:39:51.155 20925-21658/com.theapp W/System.err: java.net.SocketTimeoutException: Read timed out
09-13 18:39:51.155 20925-21658/com.theapp W/System.err: at com.android.org.conscrypt.NativeCrypto.SSL_read(Native Method)
09-13 18:39:51.155 20925-21658/com.theapp W/System.err: at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:690)
09-13 18:39:51.155 20925-21658/com.theapp W/System.err: at java.io.BufferedInputStream.read(BufferedInputStream.java:283)
09-13 18:39:51.165 20925-21658/com.theapp W/System.err: at com.android.okhttp.internal.http.UnknownLengthHttpInputStream.read(UnknownLengthHttpInputStream.java:39)
09-13 18:39:51.165 20925-21658/com.theapp W/System.err: at java.io.InputStreamReader.read(InputStreamReader.java:233)
09-13 18:39:51.165 20925-21658/com.theapp W/System.err: at java.io.BufferedReader.fillBuf(BufferedReader.java:145)
09-13 18:39:51.165 20925-21658/com.theapp W/System.err: at java.io.BufferedReader.readLine(BufferedReader.java:397)
09-13 18:39:51.165 20925-21658/com.theapp W/System.err: at com.theapp.Main.getMapData(Main.java:637)
09-13 18:39:51.165 20925-21658/com.theapp W/System.err: at com.theapp.Main$2$2.run(Main.java:231)
09-13 18:39:51.165 20925-21658/com.theapp W/System.err: at java.lang.Thread.run(Thread.java:841)
09-13 18:39:51.165 20925-21658/com.theapp D/ZZZ: EXCEPTION
'readLine()'은 데이터가 없으면'IOException'을 발생시키지 않습니다. null를 돌려줍니다. 너 무슨 소리 야? – EJP
예외 스택 추적을 게시 할 수 있습니까? –
대기중인대로 버퍼에 데이터가 전혀없는 경우가 발생합니다. 직접 테스트하지 않고 부정적으로 평가할 필요가 없습니다. 일부 Log.d()를 앞뒤에 넣었으므로 의심의 여지가 없습니다. 200을 보내면 데이터가 잘 전달되지만 데이터가없는 200을 만들면 두 번째 Lod.d()에 도달하지 않습니다. 잠시 후 IOException으로 바로갑니다. – FlorianB