0

I'm running an HTTPUrlConnection within an AsyncTask in order to retrieve JSON data from an API, but it just seems to fail and crash my app without ever hitting my catch/finally code. Of note, the URL is https, and if I change it to be http I do get an error about cleartext communication not being allowed (and this is the same if I use any URL for this connection, even just https://www.google.com vs http://www.google.com, so maybe there's something to that).

In any case, I'm at a loss for how to proceed. If I put logging in place at every line, this stops and crashes at the getResponseCode() line, and if I remove that then it stops and crashes at the getInputStream() line right afterwards. There's no difference if I remove the timeouts, either. Any help would be appreciated.

public static String getResponseFromHttpUrl(URL url) throws IOException {
    HttpURLConnection urlConnection = null;
    try {
        urlConnection = (HttpURLConnection) url.openConnection();

        urlConnection.setConnectTimeout(500);
        urlConnection.setReadTimeout(500);

        int statusCode = urlConnection.getResponseCode();

        InputStream in = urlConnection.getInputStream();

        Scanner scanner = new Scanner(in);

        scanner.useDelimiter("\\A");

        boolean hasInput = scanner.hasNext();

        if (hasInput) {
            return scanner.next();
        } else {
            return null;
        }
    }
    catch(Exception ex){
        Log.d("NetworkUtils", "error in HTTP request");
        return null;
    }
    finally {
        Log.d("NetworkUtils", "before disconnect");
        if(urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}
notnot
  • 4,472
  • 12
  • 46
  • 57
  • in every case, the `finally` branch does not return a `String`, as it should. – Martin Zeitler Oct 09 '18 at 16:55
  • The compiler doesn't seem to care because all of the other code paths have a return. – notnot Oct 09 '18 at 17:01
  • this changes nothing to the fact that the method does not match it's signature. – Martin Zeitler Oct 09 '18 at 17:04
  • This is exactly how finally is supposed to work, there doesn't need to be another return statement: https://stackoverflow.com/questions/65035/does-a-finally-block-always-get-executed-in-java – notnot Oct 09 '18 at 17:10
  • It also doesn't affect anything in this case, because in the case when I use an http instead of https URL, I return a null from the catch block and code execution proceeds. The issue is entirely some sort of crash within the try block. – notnot Oct 09 '18 at 17:12
  • have a look at the basics... https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html while the example you've provided has absolutely nothing to do with type signatures. there at least would have to be a `return null;` after the `finally` branch. – Martin Zeitler Oct 09 '18 at 18:16
  • I guess I'll have to tell the java compiler that it's wrong for complaining about the unreachable statement when I put a return statement after the finally block... this really doesn't work the way you think it does. – notnot Oct 09 '18 at 18:51
  • you are attempting to teach me a wrongful concept of the language... while I do not believe in your misconceptions. `finally` only finalizes the `try/catch` block, not the method. better stop believing and learn how to debug: https://www.jetbrains.com/help/idea/2018.1/debugging-code.html (this should also easily proof, that I'm correct by calling it a misconception). – Martin Zeitler Oct 09 '18 at 19:04
  • You're right that finally doesn't end the method, but if every branch within the try/catch has a return statement, then there isn't anything further required and it's perfectly valid code. None of your links have to do with the finally block, which acts completely differently from other structures within java.The java compiler agrees with me - go try it for yourself. – notnot Oct 09 '18 at 19:08
  • just did so and the inline code inspection instantly complained about `missing return statement` (it is pretty obvious). the rather not so obvious part is, that `urlConnection.getResponseCode()` might throw a `NetworkOnMainThreadException` - unless being run as `AsyncTask`. – Martin Zeitler Oct 09 '18 at 19:24

0 Answers0