2016-09-14 3 views
-2

HttpsURLConnection과 연결 한 후 입력 버퍼를 읽습니다. 반환 된 데이터가 비어있는 경우 (0 바이트의 경우 200 만) reader.readLine()은 IOException을 발생시킵니다. (편집 : 사실은 타임 아웃입니다.)BufferedReader.readLine() : 데이터가 비어있는 경우 예외를 피하는 방법?

사전에 이것을 알아 들으려면 어떤 if 문을 reader.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 
+3

'readLine()'은 데이터가 없으면'IOException'을 발생시키지 않습니다. null를 돌려줍니다. 너 무슨 소리 야? – EJP

+0

예외 스택 추적을 게시 할 수 있습니까? –

+0

대기중인대로 버퍼에 데이터가 전혀없는 경우가 발생합니다. 직접 테스트하지 않고 부정적으로 평가할 필요가 없습니다. 일부 Log.d()를 앞뒤에 넣었으므로 의심의 여지가 없습니다. 200을 보내면 데이터가 잘 전달되지만 데이터가없는 200을 만들면 두 번째 Lod.d()에 도달하지 않습니다. 잠시 후 IOException으로 바로갑니다. – FlorianB

답변

0

원인은 :

콘텐츠 길이가 서버에없는 데이터로 전송되지 않았습니다.

이 경우 Content-Length를 보내야하는지 여부가 명확하지 않으므로 모호한 문제인 것처럼 보입니다. 웹 브라우저는 Content-Length가 없다는 것을 잘 처리하며 0을 의미한다고 가정하는 것처럼 보입니다. 그러나 Android HttpsURLConnection은 시간 초과가 될 때까지 기다리지 않고 계속 대기합니다. 우리는 데이터가 204 HTTP 응답 코드 및 없어야한다고 주장 할 수 있습니다하지 않는 사람이 같은 문제가 그렇다면 200

:

  • 어느 것이 서버가 콘텐츠 길이를 전송해야합니다 : 0 본체가 비어 있습니다.

  • 또는이 경우 204를 보냅니다.