웹 서비스에서 계속 데이터를 가져옵니다.연결이 강제로 끊어지면 BufferedReader.readLine()이 중단됩니다.
URLConnection connection;
BufferedReader in = null;
try {
SharedPreferences preferences = context.getSharedPreferences(
"MyPreferences", Context.MODE_PRIVATE);
int timeoutConnection = Integer.parseInt(preferences.getString(
"timeout", "60")) * 1000;
URL urlAddress = new URL(preferences.getString("apiUrl",
defaultURL));
connection = urlAddress.openConnection();
connection.setConnectTimeout(timeoutConnection);
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
final String[] inputLine = { null };
int i=0;
while (isOnline() && (inputLine[0] = in.readLine()) != null) { // isOnline checks if connected to internet.
((Activity) context).runOnUiThread(new Runnable() {
public void run() {
callback.run(inputLine[0]);
}
});
}
} catch (Exception e) {
..
..
}
인터넷에 연결될 때까지 제대로 작동합니다. 그러나 강제로 연결을 끊으면 inputLine[0] = in.readLine()
이 응답하지 않습니다. 나는 예외가 없다.
그러면 어떻게 연결이 끊어 졌는지 확인할 수 있습니까? readLine()
만 사용하거나 다른 것을 사용하십시오.
참고 : BufferedReader.ready()
을 사용하는 것이 좋습니다 몇 가지 솔루션을 보았습니다. 나는 그것을 시도했지만 항상 false를 반환합니다.
해결책을 제공해주십시오.
덕분에
'URLConnection'에서 읽은 시간 초과 값을 확인할 수 있습니다. 아마도 낮은 값을 설정하면 이전 예외가 트리거됩니다. –
timeout은 45 초로 설정되어 있지만 45 초 후에도 아무 것도 표시되지 않습니다. – Nitish