Q: AsyncTask takes forever to fetch result I am making an android application where I need to fetch data from an online database. The online database has 3 columns, Date, Time and Varchar(255).
Here is my PHP code:
<?php
require "connection.php";
if($result = mysqli_query($conn, "select * from `xuberance_updates`")) {
while($row = mysqli_fetch_assoc($result)) {
    echo $row["date"].",".$row["time"].",".$row["updates"].";";
}
}
?>

And here is my kotlin code in android:
class RefreshTask(private val alertDialog: AlertDialog) :
    AsyncTask<String, Void, String>() {

    override fun onPreExecute() {
        ...
        alertDialog.show()
        ...
    }

    override fun doInBackground(vararg params: String?): String? {
        val url = URL("https://.../show_update.php")
        val httpURLConnection = url.openConnection() as HttpURLConnection
        httpURLConnection.doInput = true
        httpURLConnection.doOutput = true
        httpURLConnection.requestMethod = "POST"
        httpURLConnection.connect()
        val reader = BufferedReader(InputStreamReader(httpURLConnection.inputStream, "UTF-8"))
        var line = reader.readLine()
        val result = StringBuilder()
        while (line != null) {
            result.append(line)
            line = reader.readLine()
        }
        reader.close()
        httpURLConnection.disconnect()
        return result.toString()
    }

    override fun onPostExecute(result: String?) {
        alertDialog.dismiss()
        ...
    }
    ...
}

When I start the AsyncTask, The dialog appears. But it never gets dismissed. The onPostExecute(result: String?) method is never reached. How to fix this?
Edit: Sometimes, It successfully reaches the onPostExecute(result: String?) method. But most of the times, I need to wait for 2 or more minutes to get to that method. 

A: Use this library to make network calls. Very simple yet efficient.
http://loopj.com/android-async-http/

A: this should be culprit 
 while (line != null) {
            result.append(line)
            line = reader.readLine()
        }

