Skip to content

Commit 2bc4447

Browse files
stanstan
authored andcommitted
NetworkHelper fix
1 parent 10cf652 commit 2bc4447

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

app/src/main/java/com/stanko/network/NetworkStateHelper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.stanko.tools.Log;
1111
import com.stanko.tools.StoppableThread;
1212

13+
import java.net.SocketTimeoutException;
14+
1315
import de.greenrobot.event.EventBus;
1416

1517
/**

app/src/main/java/com/stanko/tools/InternetConnectionHelper.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ public static boolean isHostReachable(String hostUrl) {
315315
hostUrl = "http://" + hostUrl;
316316

317317
boolean isReachable = false;
318-
boolean isExceptionHappens = false;
318+
// boolean isExceptionHappens = false;
319319
try {
320320
final URL url = new URL(hostUrl);
321321
final HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
@@ -324,19 +324,22 @@ public static boolean isHostReachable(String hostUrl) {
324324
httpURLConnection.setConnectTimeout(TIME_OUT); // Timeout in seconds
325325
httpURLConnection.connect();
326326
final int responseCode = httpURLConnection.getResponseCode();
327-
isReachable = responseCode == HttpURLConnection.HTTP_OK //200
328-
|| responseCode == HttpURLConnection.HTTP_BAD_METHOD
329-
|| responseCode == HttpURLConnection.HTTP_BAD_REQUEST
330-
|| responseCode == HttpURLConnection.HTTP_ACCEPTED
331-
|| responseCode == HttpURLConnection.HTTP_FORBIDDEN
332-
|| responseCode == HttpURLConnection.HTTP_UNAUTHORIZED;
327+
isReachable = responseCode >0;
328+
// https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
329+
// there are too many codes, guess checking if its gt 0 is enough
330+
// == HttpURLConnection.HTTP_OK //200
331+
// || responseCode == HttpURLConnection.HTTP_BAD_METHOD
332+
// || responseCode == HttpURLConnection.HTTP_BAD_REQUEST
333+
// || responseCode == HttpURLConnection.HTTP_ACCEPTED
334+
// || responseCode == HttpURLConnection.HTTP_FORBIDDEN
335+
// || responseCode == HttpURLConnection.HTTP_UNAUTHORIZED;
333336

334337
} catch (MalformedURLException e) {
335-
isExceptionHappens = true;
336-
Log.e("ServerChecker", e);
338+
// isExceptionHappens = true;
339+
// Log.e("ServerChecker", e);
337340
} catch (IOException e) {
338-
isExceptionHappens = true;
339-
Log.e("ServerChecker", e);
341+
// isExceptionHappens = true;
342+
// Log.e("ServerChecker", e);
340343
}
341344

342345
// // maybe host is not reachable using GET method? Lets try with POST

0 commit comments

Comments
 (0)