0

I have a function that checks network connection then check server availability. If there's network connection it will next check server availability. Unfortunately, checking server availability is through AsyncTask.

This is how I want to use the AsyncTask:

if(NetworkConnectionInfo(context).execute()) { 
    return true 
} else { 
   return false 
}

this is the NetworkConnectionInfo class

    class NetworkConnectionInfo : AsyncTask<String, String, Boolean> {

    private var context: Context? = null

    constructor(context:Context):super(){
        this.context = context
    }

    override fun onPreExecute() {}

    override fun doInBackground(vararg p0: String?): Boolean {
        try {
            val url = URL("http://www.example.com/")
            val urlc = url.openConnection() as HttpURLConnection
            urlc.setRequestProperty("User-Agent", "test")
            urlc.setRequestProperty("Connection", "close")
            urlc.setConnectTimeout(1000) // mTimeout is in seconds
            urlc.connect()

            return urlc.getResponseCode() === 200

        } catch (ex:Exception) {
            ex.printStackTrace()
        }
        return false
    }

    override fun onProgressUpdate(vararg values: String?) {}

    override fun onPostExecute(success: Boolean) {
        if(!success) {
            Toast.makeText(this.context,"Error connecting server. Please try again later.", Toast.LENGTH_LONG).show()
        } else {
            Toast.makeText(this.context,"Server is available.", Toast.LENGTH_LONG).show()
        }
    }
}

I want to return success in onPostExecute. I don't know how to approach this.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Heero Yuy
  • 614
  • 9
  • 24

1 Answers1

1

An AsyncTask does not have a return value, since it is asynchronous.

You have to use the result (success) directly in the callback function onPostExecute. This is how AsyncTask is designed and supposed to be used.

override fun onPostExecute(success: Boolean) {
   // call further functions depending on "success"
   // Note: can access views since it runs on the UI thread
}
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
  • I will know the status of the server in the onPostExecute, but in my function where it has this line `NetworkConnectionInfo(context).execute()` I want to get the result if it's true or false because i want to use it. the function is is `if(isServerIsAvailable()){ //do this}else{//do this one}` – Heero Yuy Jan 03 '18 at 13:24
  • 1
    You cannot, because it runs on a different thread! Whatever code you want to run in the true branch, run it directly in onPostExecute! – Willi Mentzel Jan 03 '18 at 13:26
  • hmm what I'm trying to achieve is kinda difficult – Heero Yuy Jan 03 '18 at 13:28
  • 1
    I think you have to restructure your code, keeping the way threads work in mind. – Willi Mentzel Jan 03 '18 at 13:28
  • You need to learn more about threads. Server availability check is done on background thread and main (UI) thread is not interrupted. You need to decide what to do while server checking is happening. Show a progressbar or show some text as toast, whatever. After network call is done, onPostExecute runs on main thread from where you can do the actual function you need to do using the server check result. – muthuraj Jan 03 '18 at 18:05