Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpURLConnection.getInputStream() hangs and never finishes

I am attempting to get a token from a JSON endpoint using the following code:

public class FetchToken extends AsyncTask<String, Void, Void> {
    String data = "";
    String token = "";

    public TokenDelegate delegate = null;

    @Override
    protected Void doInBackground(String... identity) {
        try {
            if (identity.length == 1) {
                URL url = new URL(identity[0]);

                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String line = "";

                while (line != null) {
                    line = bufferedReader.readLine();
                    data = data + line;
                }

                JSONObject JO = new JSONObject(data);
                token = JO.get("token").toString();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        Log.d("token", token);
        //delegate.processFinish(token);
        super.onPostExecute(aVoid);
    }
}

I have tried printing to the debug log after every line, and it seems that the line InputStream inputStream = httpURLConnection.getInputStream(); never finishes, nor does the rest of the code get executed. However, this is taking place inside of an AsyncTask, and the onPostExecute method of that AsyncTask is firing as if the task has been completed, which it doesn't seem as if it has been.

None of the catch blocks are being executed, it seems like, and when I inspect the Network panel in the Android Profiler then the request says it is not passing anything back. However, accessing the JSON endpoint in browser brings everything back correctly.

Hopefully someone knows what the problem may be. I've attempted to google things to no avail. Let me know if more information is needed. Thanks.

like image 559
Dehodson Avatar asked Dec 10 '22 07:12

Dehodson


2 Answers

You most likely want to set timeouts to ensure that the connection fails when the external resource isn't available in timely manner. The values are in milliseconds e.g. 5 sec timeout for both connect and read would be:

 HttpURLConnection conn = ...
 conn.setConnectTimeout(5000);
 conn.setReadTimeout(5000);

By default the timeouts are set to 0 and as per setConnectTimeout() this means:

A timeout of zero is interpreted as an infinite timeout.

like image 74
Karol Dowbecki Avatar answered Dec 30 '22 00:12

Karol Dowbecki


The line InputStream inputStream = httpURLConnection.getInputStream(); was never completing because it was throwing a java.security.cert.CertPathValidatorException. The error was not being logged because I was not catching this type of error in my try / catch block.

like image 31
Dehodson Avatar answered Dec 30 '22 02:12

Dehodson