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();
        }
    }
}
